不废话,直接上代码
// 实例化
$criteria = new CDbCriteria;
$criteria->addCondition("id=1"); // 表示 where id = 1
$criteria->addInCondition('id', array(1, 2, 3, 4, 5)); // 表示 where id IN (1,2,3,4,5)
$criteria->addCondition('id = 1', 'OR'); // 表示 这里是 OR 条件
$criteria->addSearchCondition('name', '张三');// 表示 where name like '%张三%'
$criteria->addBetweenCondition('id', 1, 4); // 表示 between 1 and 4
$criteria->compare('id', 1); // 表示 where id = 1
$criteria->compare('id', 1, true); // 表示 where id like '%1%'
// 参数绑定
$criteria->addCondition('id=:id');
$criteria->params[':id'] = 1;
$criteria->select = 'id, name'; // 要查询的字段,默认 select = '*' 也可用 array('id', 'name')
$criteria->join = 'left join user as u on u.id = t.id'; // 连接表
$criteria->with = 'xxx'; // 调用 relations
$criteria->limit = 10; // 取10数
$criteria->offset = 1; // 两条合并起来,则表示 limit 10 offset 1,或者代表了 limit 1, 10
$criteria->order = 'id DESC, name ASC'; //排序条件
$criteria->group = 'name'; // group 分组
$criteria->having = 'SUM(revenue) < 50000'; // having 条件
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 15 // 页数
),
'sort' => array(
'defaultOrder' => 'id DESC' // 默认排序
),
));