This article mainly introduces common YiiCDBCriteria methods, and summarizes and analyzes the functions and common methods of the CDBCriteria class based on the instance form, which has some reference value, for more information about Yii CDBCriteria, see the following example. We will share this with you for your reference. The details are as follows:
Note: $ c = new CDbCriteria (); is a way of writing ActiveRecord, making ActiveRecord more flexible, rather than DAO (PDO) and Query Builder in the manual.
A few comments: I feel like this manual is generally done.
Link: http://www.yiiframework.com/doc/api/1.1/CDbCriteria
This is some notes and common usage of Yii CDbCriteria:
I. SQL statement Assembly
Php code:
$ Criteria = new CDbCriteria; // function method $ criteria-> addCondition ("id = 1"); // query condition, where id = 1 $ criteria-> addInCondition ('id', array (, 5); // represents where id IN ,); $ criteria-> addNotInCondition ('id', array (1, 2, 3, 4, 5); // The method is the same as the preceding method, is not in $ criteria-> addCondition ('Id = 1', 'OR'); // This is an OR condition. when multiple conditions exist, this condition is OR rather than AND $ criteria-> addSearchCondition ('name', 'classified'); // The search condition actually represents .. Where name like '% Category %' $ criteria-> addBetweenCondition ('id', 1, 4); // between 1 and 4 $ criteria-> compare ('id ', 1); // This method is special. it will be automatically processed as addCondition or addInCondition based on your parameters, // if the second parameter is an array, addInCondition $ criteria-> addCondition ("id =: id"); $ criteria-> params [': id'] = 1; // attribute method $ criteria-> select = 'id, parentid, name'; // indicates the field to be queried. the default value is select = '*'; $ criteria-> join = 'XXX'; // connection table $ criteria-> with = 'XXX'; // call relations $ cri Teria-> limit = 10; // Obtain 1 piece of data. if the value is smaller than 0, $ criteria-> offset = 1 is not processed. // The two pieces are merged, it indicates the limit 10 offset 1, or represents. Limit $ criteria-> order = 'XXX DESC, xxx ASC '; // sorting condition $ criteria-> group = 'group condition '; $ criteria-> having = 'having condition '; $ criteria-> distinct = FALSE; // whether the query is unique
Instance:
Php code:
$ Criteria = new CDbCriteria (); $ criteria-> select = 'Table _ name, model_id, sum (amount) total'; $ criteria-> group = 'Table _ name, model_id '; $ criteria-> addCondition ("$ nIdcId = 4"); // You can 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-> condition. = "AND t. id> $ last_log_id ";}$ c-> limit = 20; $ c-> order ='t. id DESC '; $ logs = OutsourceProcessLog: model ()-> findAll ($ c );
Annotation:
1. The result is different from the DAO method: each element in the DAO method array is still an array. In the CDbCriteria mode, 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, you still need SQL statements. For example, select avg (num) amount from ****.
3. print the running results to see how yii achieves this.
Php code:
// Visible $ c = new CDbCriteria (); $ c-> join = "JOIN idc_user on t. id = idc_user.user_id "; $ c-> condition =" idc_user.idc_id = $ idc_id "; // after running the object (CDbCriteria) #98 (12) {["select"] => string (1) "*" ["distinct"] => bool (false) ["condition"] => string (17) "idc_user.idc_id = 6" ["params"] => array (0) {} ["limit"] => int (-1) ["offset"] => int (-1) ["order"] => string (0) "" ["group"] => string (0) "" ["join"] => string (38) "JOIN idc_user on t. id = idc_user.user_id "[" having "] => string (0)" "[" with "] => NULL [" alias "] => NULL} // User :: model ()-> with ('idcs ')-> findAll ($ c)
II. mergeWith
Php code:
// IN ActiveRecord, add the condition $ this-> getDbCriteria ()-> mergeWith (array ('condition '=> "idc_id IN ($ ids )",));
I hope this article will help you design PHP programs based on the Yii Framework.
For more information about examples of common Yii CDBCriteria methods, see PHP!