<?php
class TestController extends CController
{
public function actionTest()
{
$criteria = new CDbCriteria();
// $criteria->addCondition('id=1');//SELECT * FROM `test1` `t` WHERE id=1
// $criteria->addCondition("name='aaa2'");//SELECT * FROM `test1` `t` WHERE id=1
//$criteria->addCondition('id=1','OR');//SELECT * FROM `test1` `t` WHERE (name='aaa2') OR (id=1)
//$criteria->addSearchCondition('name','aa');//SELECT * FROM `test1` `t` WHERE name LIKE :ycp0
// $criteria->addBetweenCondition('id',1,2);//SELECT * FROM `test1` `t` WHERE id BETWEEN :ycp0 AND :ycp1
//这个方法比较特殊,他会根据你的参数自动处理成addCondition或者addInCondition,
//即如果第二个参数是数组就会调用addInCondition
//$criteria->compare('id',1);//SELECT * FROM `test1` `t` WHERE id=:ycp0
// $criteria->compare('id',array(1,2));//SELECT * FROM `test1` `t` WHERE id IN (:ycp0, :ycp1)
$criteria->select = 'id,name';//SELECT id,name FROM `test1` `t` WHERE id=:id
$criteria->limit = 10;//SELECT id,name FROM `test1` `t` WHERE id=:id
$criteria->offset = 0;//SELECT id,name FROM `test1` `t` WHERE id=:id
$criteria->addCondition('id=:id');//SELECT * FROM `test1` `t` WHERE id=:id
$criteria->params[':id'] = 1;
// $criteria->addInCondition('id',array(1,2,3));//SELECT * FROM `test1` `t` WHERE id IN (:ycp0, :ycp1, :ycp2)
//$criteria->addNotInCondition('id',array(1,2,3));//SELECT * FROM `test1` `t` WHERE id NOT IN (:ycp0, :ycp1,:ycp2)
var_dump($criteria);
$ret = Test::model()->findAll($criteria);
var_dump($ret);
Yii::app()->end();
}
}
41. Yii CDBCritiria
最新推荐文章于 2017-09-09 23:57:43 发布