Yii cdbcriteria common methods

Source: Internet
Author: User

Note: $ c = new cdbcriteria (); is a way of writing activerecord, making activerecord more flexible, rather than Dao (PDO) and Query Builder in the manual.

This is some notes and common usage of yii cdbcriteria:

I. SQL statement assembly

$ Criteria = new cdbcriteria;

$ Criteria-> addcondition ("ID = 1"); // query condition, that is, where id = 1

$ Criteria-> addincondition ('id', array (, 5); // represents where ID in ,);

$ Criteria-> addnotincondition ('id', array (, 5); // It is in the same phase as above, not in

$ Criteria-> addcondition ('Id = 1', 'or'); // This is an or condition. When multiple conditions exist, the condition is or rather than and.

$ Criteria-> addsearchcondition ('name', 'category'); // 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 and will be automatically processed as addcondition or addincondition according to your parameters,

// If the second parameter is an array, addincondition is called.

$ 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

$ Criteria-> Limit = 10; // obtain 1 piece of data. If the value is smaller than 0, no processing is performed.

$ Criteria-> offset = 1; // if the two parameters are merged, the limit 10 offset 1 is used, or the limit 10 offset 1 is used. Limit 1, 10

$ 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 difference from the DAO method is that every 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 ****.


Ii. mergewith

// Add conditions in activerecord

$ This-> getdbcriteria ()-> mergewith (Array (

'Condition' => "idc_id in ($ IDs )",

));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.