加载中...

Yii-模型- criteria查找数据库方法


数据模型搜索方法:

  1. public function search()
  2. {
  3. // Warning: Please modify the following code to remove attributes that
  4. // should not be searched.
  5.  
  6. $criteria=new CDbCriteria;
  7.  
  8. $criteria->compare('id',$this->id);
  9. $criteria->compare('title',$this->title,true); //支持模糊查找
  10. $criteria->compare('content',$this->content,true); //支持模糊查找
  11. $criteria->compare('type',$this->type);
  12. $criteria->compare('user',$this->user,true); //支持模糊查找
  13. $criteria->compare('status',$this->status);
  14. $criteria->compare('create_data',$this->create_data,true); //支持模糊查找
  15.  
  16. return new CActiveDataProvider($this, array(
  17. 'criteria'=>$criteria,
  18. 'pagination'=>array(
  19. 'pageSize'=>50,
  20. ),
  21. ));
  22. }


定义比较运算
  1. $criteria->compare('create_time','<='.$this->endtime),//创建早间小于等于指定时间

定义要查找的字段

  1. //查找的结果
  2. $criteria->select = 'id,title,content,author,status,createtime',
  3. //也可以以下一种方式定义
  4. $criteria->select = array('id','title','content','author','status','createtime'),

定义填加查找条件

  1. //定义条件
  2. $criteria->select = 'status=1',
  3. //添加匹配
  4. $criteria->compare('title',$this->title,true),
  5. //添加条件 $condition可以是数组,也可以是字符串,and可以省略
  6. $criteria->addCondition($condition,'and'),
  7. //添加IN条件 $column为字段名
  8. $criteria->addInCondition(string $column, array $values, string $operator='AND')
  9. //添加notin条件
  10. $criteria->addNotInCondition(string $column, array $values, string $operator='AND')
  11. //添加like条件
  12. $criteria->addSearchCondition(string $column, string $keyword),
  13. //添加Between条件
  14. $criteria->addBetweenCondition(string $column, string $valueStart, string $valueEnd, string $operator='AND'),

JOIN连表查询

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

order查询结果排序

  1. $criteria->order = 'createtime DESC',

group结果分组

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

having筛选分组结果分组数量

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

  1.  

还没有评论.