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 的方法实现。虽然此方法看起来很诱人, 但它经常引发混淆,冲突和好比列名大小写敏感的问题。php

当有多行数据匹配指定的查询条件时,咱们能够经过下面的 findAll 方法将他们所有带回。 每一个都有其各自的 find 方法,就像咱们已经讲过的那样。sql

// 查找知足指定条件的全部行
$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);
 
若是没有任何东西符合查询条件, 将返回一个空数组。这跟  不一样, 会在没有找到什么东西时返回 null。
findAllfindfind
除了上面讲述的  和  方法,为了方便,(Yii)还提供了以下方法:findfindAll
// 获取知足指定条件的行数
$n=Post::model()->count($condition,$params);
// 经过指定的 SQL 获取结果行数
$n=Post::model()->countBySql($sql,$params);
// 检查是否至少有一行复合指定的条件
$exists=Post::model()->exists($condition,$params);
相关文章
相关标签/搜索