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',