• yii 数据库 Active Record


    // 查找满足指定条件的结果中的第一行
    $post=Post::model()->find($condition,$params);
    // 查找具有指定主键值的那一行
    $post=Post::model()->findByPk($postID,$condition,$params);
    // 查找具有指定属性值的行
    $post=Post::model()->findByAttributes($attributes,$condition,$params);
    // 通过指定的 SQL 语句查找结果中的第一行
    $post=Post::model()->findBySql($sql,$params);
    $criteria=new CDbCriteria;
    $criteria->select='title';  // 只选择 'title' 列
    $criteria->condition='postID=:postID';
    $criteria->params=array(':postID'=>10);
    $post=Post::model()->find($criteria); // $params 不需要了
    $post=Post::model()->find(array(
        'select'=>'title',
        'condition'=>'postID=:postID',
        'params'=>array(':postID'=>10),
    ));

    信息: 当一个查询条件是关于按指定的值匹配几个列时,我们可以使用 findByAttributes()。我们使$attributes 参数是一个以列名做索引的值的数组。在一些框架中,此任务可以通过调用类似findByNameAndTitle 的方法实现。虽然此方法看起来很诱人, 但它常常引起混淆,冲突和比如列名大小写敏感的问题。

    当有多行数据匹配指定的查询条件时,我们可以通过下面的 findAll 方法将他们全部带回。 每个都有其各自的 find 方法,就像我们已经讲过的那样。

    // 查找满足指定条件的所有行
    $posts=Post::model()->findAll($condition,$params);
    // 查找带有指定主键的所有行
    $posts=Post::model()->findAllByPk($postIDs,$condition,$params);
    // 查找带有指定属性值的所有行
    $posts=Post::model()->findAllByAttributes($attributes,$condition,$params);
    // 通过指定的SQL语句查找所有行
    $posts=Post::model()->findAllBySql($sql,$params);
    
    
    如果没有任何东西符合查询条件,findAll 将返回一个空数组。这跟 find 不同,find 会在没有找到什么东西时返回 null。
    
    除了上面讲述的 find 和 findAll 方法,为了方便,(Yii)还提供了如下方法:
    // 获取满足指定条件的行数
    $n=Post::model()->count($condition,$params);
    // 通过指定的 SQL 获取结果行数
    $n=Post::model()->countBySql($sql,$params);
    // 检查是否至少有一行复合指定的条件
    $exists=Post::model()->exists($condition,$params);

     

  • 相关阅读:
    【CodeForces】889 C. Maximum Element 排列组合+动态规划
    【CodeForces】889 B. Restoration of string
    使用torchsummary打印torch模型结构,包括每层名字以及形状
    任意角度的场景文本检测论文简单总结
    vi快捷键
    sklearn框架使用例子,多种分类方法的集合,方便模型的融合
    卷积核的参数量和计算量
    windows下shell命令行的常用操作命令
    MARKDOWM书写规范
    keras使用horovod多gpu训练
  • 原文地址:https://www.cnblogs.com/sanpoye/p/3700965.html
Copyright © 2020-2023  润新知