Example of Yii CDbCriteria query condition usage

Source: Internet
Author: User
This article mainly introduces the usage of CDbCriteria query conditions in Yii. The example summarizes the common usage of CDbCriteria query conditions, which has some reference value for database program design using Yii, for more information about Yii CDbCriteria, see the examples in this article. The specific analysis is as follows:

Here are the various conditions for using the CDbCriteria method to query in Yii:

The code is as follows:

$ Criteria = new CDbCriteria;
$ Criteria-> addCondition ("MACID = 464"); // 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 ('link', 'noise'); // search criteria where name like '% Category %'
$ Criteria-> addBetweenCondition ('id', 10000,100 05); // between 10000 and 10005

$ Criteria-> compare ('id', 1); // This method is special and will be based on your parameters
// It is automatically processed as addCondition or addInCondition,
// If the second parameter is an array, addInCondition is called.
/**
* Passing variables
*/
$ Criteria-> addCondition ("id =: id ");
$ Criteria-> params [': id'] = 10;
/**
* Some public vars
*/
$ Criteria-> select = 'id, MACID, type'; // 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 = 'Id DESC, macid asc '; // sorting condition
$ Criteria-> group = 'group type ';
$ Criteria-> having = 'having condition ';
$ Criteria-> distinct = FALSE; // whether the query is unique
Return new CActiveDataProvider ('visitlog', array (
'Criteria '=> $ criteria,
));

I hope this article will help you design PHP programs based on the Yii Framework.

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.