ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# CDbCriteria类 https://www.yiichina.com/doc/api/1.1/CDbCriteria It can be used in AR query methods such as CActiveRecord::find and CActiveRecord::findAll. ``` $criteria=new CDbCriteria(); $criteria->compare('status',Post::STATUS_ACTIVE); $criteria->addInCondition('id',array(1,2,3,4,5,6)); $posts = Post::model()->findAll($criteria); ``` ![]('./img/2018-11-20_134138.png') ![]('./img/CDbCriteria.png') ## 使用实例 https://www.yiichina.com/doc/guide/1.1/database.ar ``` $criteria=new CDbCriteria; $criteria->select='title'; // 只选择 'title' 列 $criteria->condition='postID=:postID'; $criteria->params=array(':postID'=>10); $post=Post::model()->find($criteria); // $params 不需要了 ``` ## 定义了关联模型的关系 D:\wamp\www\weike.test\weiketest\protected\models\weike\wkeTeachingV3.php public function relations() { return array( 'prepare' => array(self::BELONGS_TO, 'wkePrepare', 'fdPrepareID'), 'class'=>array(self::BELONGS_TO,'wkeClass','fdClassID') ,//by liupf 2016/7/9 'record'=>array(self::HAS_MANY,'wkeTeachingV3Student','fdTeachingV3ID'),//by liupf 2016/9/28 'content'=>array(self::BELONGS_TO,'Content','fdPrepareID'),//by liupf 2016/9/28 'user'=>array(self::BELONGS_TO,'User','fdTeacherID'),//by liupf 2016/10/19 'operations' => array(self::HAS_MANY,'wkeTeachingV3Operation','fdTeachingV3ID') , 'schoolMap'=>array(self::HAS_ONE,'SchoolMap','','on'=>'t.fdTeacherID = schoolMap.fdUserID'), ); } ## CDbCriteria对象的参数设置 if($args['prepareName']){ $with[]='content'; $criteria->addSearchCondition('content.fdName',$args['prepareName']); } content.fdName LIKE "%正数%" if($args['keyword'] || (0===$args['keyword'] || '0'===$args['keyword'])){ $with[]='user'; $with[]='content'; $criteria->addCondition("content.fdName LIKE '%".$args['keyword']."%' OR user.fdRealName LIKE '%".$args['keyword']."%'"); }