Home >Backend Development >PHP Tutorial >Summary of commonly used CDbCriteria in Yii, yiicdbcriteria_PHP tutorial
Reprinted from: http://www.cnblogs.com/mrcoke/articles/Yii.html
Yii’s Active Recorder comes packed with a lot.
In particular, include common phrases such as where, order, limit, IN/not IN, like, etc. in SQL into the CDbCriteria class, so that the entire code will be more standardized and clear at a glance.
$criteria =newCDbCriteria;
$criteria->addCondition("id=1"); //Query conditions, that is, where id =1
$criteria->addInCondition('id', array(1,2, 3,4,5));//Represents where id IN (1,23,,4,5,);
$criteria->addNotInCondition('id',array(1,2,3,4, 5));//Exactly the same as the above, it is NOT IN
$criteria->addCondition('id=1','OR');//This is an OR condition. When there are multiple conditions, the The condition is OR instead of AND
$criteria->addSearchCondition('name','category');//The search conditions actually represent. . where name like '%category%'
$criteria->addBetweenCondition('id', 1, 4);//between1 and 4
$criteria->compare('id',1 ; addCondition("id = :id");
$criteria->params[':id']=1;
$criteria->select = 'id,parentid,name';/ / represents the field to be queried, the default select='*';
$criteria->join = 'xxx'; //Join table
$criteria->with = 'xxx';//Call relations
$criteria->limit =10; //Take 1 piece of data, if it is less than 0, do not process it
$criteria->offset =1; //Combined two pieces of data, it means limit 10 offset1, or represents. limit 1,10
$criteria->order = 'xxx DESC,XXX ASC' ;//Sort conditions
$criteria->group = 'group condition';
$criteria->having = 'having conditions';
$criteria->distinct = FALSE; // Is it a unique query
If you only need to select specific fields, you can use the following method
model()->find(array('select' => 'Field name 1, field name 2', 'condition ' => 'Selection condition'))
findAll is similar
Yii CDbCriteria supports stored procedures