Home  >  Article  >  Backend Development  >  Yii CDbCriteria_PHP Tutorial

Yii CDbCriteria_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:59944browse

Note: $c = new CDbCriteria(); is a way of writing ActiveRecord, making ActiveRecord more flexible, instead of DAO (PDO) and Query Builder in the manual.

These are some notes and common usage of Yii CDbCriteria:

1. The case of a sql assembly

$criteria = new CDbCriteria; >
 

$criteria

->addCondition("id=1"); //Query conditions, i.e. 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 the OR condition. When there are multiple conditions, the condition is OR instead of AND.  

$criteria

->addSearchCondition('name', 'category' );//Search conditions actually represent. . where name like '%category%'  

$criteria

->addBetweenCondition('id', 1, 4);//between 1 and 4  

 

$criteria

->compare('id', 1);  //This method is quite special, it will automatically process it into addCondition or addInCondition according to your parameters, // That is, if the second parameter is the array, the adDInCondition

 

 $criteria

->addCondition("id = :id");   $criteria

->params[':id']=1;   

//Attribute method

 $criteria

->select = 'id,parentid,name'; //Represents the field to be queried, default select='*'; $criteria

->join = 'xxx'; //join Table $criteria

->with = 'xxx'; //Call relations $criteria

->limit = 10; //Get 1 piece of data, if less than 0, no processing will be done $criteria

->offset = 1; //When the two are combined, it means limit 10 offset 1, or represents . limit 1,10 $criteria

->order = 'xxx DESC,XXX ASC' ;//Sort conditions

 $criteria->group = 'group criteria'

 $criteria->having = 'having conditions '

$criteria->distinct = FALSE; //Is it a unique query?




Example:

Php code

$criteria = new CDbCriteria();

$criteria->select = 'table_name,model_id,sum(amount) total';

$criteria->group = 'table_name,model_id';

$criteria->addCondition("$nIdcId=4");//Also $ criteria->condition = "$nIdcId=4";

$aResult = accessory_info::model()->findAll($criteria);

$c = new CDbCriteria();

$c->select = 't.id, t.created_at, t.outsource_id, t.user_id, t.operate, t.content' ;

$c->join = 'LEFT JOIN outsource ON outsource.id=t.outsource_id';

$c->condition = 'outsource.idc_id IN(' .implode(',' , $idc_ids) . ')';

if($last_log_id) { 

> 🎜>

$c

->limit = 20;

$c->order =

't.id DESC';

$logs = OutsourceProcessLog::model()->findAll($c

);

Annotations: 1. The difference from the DAO method is that each element in the DAO method is still an array. With the CDbCriteria method, the elements in the array are objects. 2. Even if this function is very powerful, there are still some requirements that cannot be met. At this time, sql statements are still needed. For example, select avg(num) amount from ****. 2. The situation of mergeWith



//In ActiveRecord, add conditional restrictions




$this

->getDbCriteria()->mergeWith(array

 'condition'=>"idc_id IN ($ids)"

)); The original text is reproduced from Baidu Space "Yii CDbCriteria Common Methods"


http://www.bkjia.com/PHPjc/780018.html

www.bkjia.com

truehttp: //www.bkjia.com/PHPjc/780018.html

TechArticle

Note: $c=newCDbCriteria(); is a way of writing ActiveRecord, making ActiveRecord more flexible, not a manual In DAO (PDO) and QueryBuilder. These are some notes and common usage of YiiCDbCriteria...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn