Method for Searching databases in criteria of Yii model operations, yiicriteria

Source: Internet
Author: User

Method for Searching databases in criteria of Yii model operations, yiicriteria

This article describes how to use criteria to search for databases in Yii model operations. We will share this with you for your reference. The details are as follows:

Data Model search method:

Public function search () {// Warning: Please modify the following code to remove attributes that // shocould not be searched. $ criteria = new CDbCriteria; $ criteria-> compare ('id', $ this-> id); $ criteria-> compare ('title', $ this-> title, true); // supports fuzzy search for $ criteria-> compare ('content', $ this-> content, true ); // support fuzzy search $ criteria-> compare ('type', $ this-> type); $ criteria-> compare ('user', $ this-> user, true); // fuzzy search is supported. $ criteria-> compare ('status', $ this-> status); $ criteria-> compare ('create _ data ', $ this-> create_data, true); // supports fuzzy search return new CActiveDataProvider ($ this, array ('Criteria '=> $ criteria, 'pagination' => array ('pagesize' => 50 ,),));}

Define comparison operations:

$ Criteria-> compare ('create _ time', '<='. $ this-> endtime), // The creation time is less than or equal to the specified time

Define the fields to be searched:

// Search result $ criteria-> select = 'id, title, content, author, status, createtime ', // You can also define $ criteria-> select = array ('id', 'title', 'content', 'author', 'status ', 'createtime '),

Define and add search conditions:

// Define conditions $ criteria-> select = 'status = 1', // Add matching $ criteria-> compare ('title', $ this-> title, true ), // Add a condition $ condition can be an array or a string, and can omit $ criteria-> addCondition ($ condition, 'and '), // Add the IN condition $ column as the field name $ criteria-> addInCondition (string $ column, array $ values, string $ operator = 'and ') // Add the notin condition $ criteria-> addNotInCondition (string $ column, array $ values, string $ operator = 'and ') // Add the like condition $ criteria-> addSearchCondition (string $ column, string $ keyword), // Add the Between condition $ criteria-> addBetweenCondition (string $ column, string $ valueStart, string $ valueEnd, string $ operator = 'and '),

JOIN Table query

$criteria->join = 'LEFT JOIN users ON users.id=authorID',

Order query result sorting:

$criteria->order = 'createtime DESC',

Group result group:

$criteria->group = 'projectID, teamID',

Having:

$criteria->having = 'SUM(revenue)<50000',

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.