Home > Backend Development > PHP Tutorial > About the method of searching the database using criteria in Yii model operation

About the method of searching the database using criteria in Yii model operation

不言
Release: 2023-04-01 12:06:02
Original
1189 people have browsed it

This article mainly introduces the method of searching the database using criteria of Yii model operation, and analyzes the instantiation and query operation related techniques of criteria in Yii model in the form of examples. Friends in need can refer to this article

The example describes the method of searching the database using criteria of Yii model operation. Share it with everyone 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
  // should not be searched.
  $criteria=new CDbCriteria;
  $criteria->compare('id',$this->id);
  $criteria->compare('title',$this->title,true); //支持模糊查找
  $criteria->compare('content',$this->content,true); //支持模糊查找
  $criteria->compare('type',$this->type);
  $criteria->compare('user',$this->user,true); //支持模糊查找
  $criteria->compare('status',$this->status);
  $criteria->compare('create_data',$this->create_data,true); //支持模糊查找
  return new CActiveDataProvider($this, array(
    'criteria'=>$criteria,
    'pagination'=>array(
      'pageSize'=>50,
    ),
  ));
}
Copy after login

Define comparison operation:

$criteria->compare(&#39;create_time&#39;,&#39;<=&#39;.$this->endtime),
//创建早间小于等于指定时间
Copy after login

Define the field to be searched:

//查找的结果
$criteria->select = &#39;id,title,content,author,status,createtime&#39;,
//也可以以下一种方式定义
$criteria->select = array(&#39;id&#39;,&#39;title&#39;,&#39;content&#39;,&#39;author&#39;,&#39;status&#39;,&#39;createtime&#39;),
Copy after login

Define the search conditions:

//定义条件
$criteria->select = &#39;status=1&#39;,
//添加匹配
$criteria->compare(&#39;title&#39;,$this->title,true),
//添加条件 $condition可以是数组,也可以是字符串,and可以省略
$criteria->addCondition($condition,&#39;and&#39;),
//添加IN条件 $column为字段名
$criteria->addInCondition(string $column, array $values, string $operator=&#39;AND&#39;)
//添加notin条件
$criteria->addNotInCondition(string $column, array $values, string $operator=&#39;AND&#39;)
//添加like条件
$criteria->addSearchCondition(string $column, string $keyword),
//添加Between条件
$criteria->addBetweenCondition(string $column, string $valueStart, string $valueEnd, string $operator=&#39;AND&#39;),
Copy after login

JOIN table query

$criteria->join = &#39;LEFT JOIN users ON users.id=authorID&#39;,
Copy after login

order query result sorting:

$criteria->order = &#39;createtime DESC&#39;,
Copy after login

group result grouping:

$criteria->group = &#39;projectID, teamID&#39;,
Copy after login

having filter grouping result group number:

$criteria->having = &#39;SUM(revenue)<50000&#39;,
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis on sphinx index configuration in Yii framework

yii2 uses GridView to implement all data selection and Batch delete button

About how to write jQuery for search paging in the YII framework

The above is the detailed content of About the method of searching the database using criteria in Yii model operation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template