本文實(shí)例講述了Yii模型操作之criteria查找數(shù)據(jù)庫的方法。分享給大家供大家參考,具體如下:
數(shù)據(jù)模型搜索方法:
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,
),
));
}
定義比較運(yùn)算:
$criteria->compare('create_time','<='.$this->endtime),
//創(chuàng)建早間小于等于指定時間
定義要查找的字段:
//查找的結(jié)果
$criteria->select = 'id,title,content,author,status,createtime',
//也可以以下一種方式定義
$criteria->select = array('id','title','content','author','status','createtime'),
定義填加查找條件:
//定義條件
$criteria->select = 'status=1',
//添加匹配
$criteria->compare('title',$this->title,true),
//添加條件 $condition可以是數(shù)組,也可以是字符串,and可以省略
$criteria->addCondition($condition,'and'),
//添加IN條件 $column為字段名
$criteria->addInCondition(string $column, array $values, string $operator='AND')
//添加notin條件
$criteria->addNotInCondition(string $column, array $values, string $operator='AND')
//添加like條件
$criteria->addSearchCondition(string $column, string $keyword),
//添加Between條件
$criteria->addBetweenCondition(string $column, string $valueStart, string $valueEnd, string $operator='AND'),
JOIN連表查詢:
$criteria->join = 'LEFT JOIN users ON users.id=authorID',
order查詢結(jié)果排序:
$criteria->order = 'createtime DESC',
group結(jié)果分組:
$criteria->group = 'projectID, teamID',
having篩選分組結(jié)果分組數(shù)量:
$criteria->having = 'SUM(revenue)<50000',
希望本文所述對大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。