如下是CDbCriteria的一些用法php
$criteria = new CDbCriteria; $criteria->addCondition("id=1"); //查詢條件,即where id = 1 $criteria->addInCondition('id', array(1,2,3,4,5)); //表明where id IN (1,23,,4,5,); $criteria->addNotInCondition('id', array(1,2,3,4,5));//與上面正好相法,是NOT IN $criteria->addCondition('id=1','OR');//這是OR條件,多個條件的時候,該條件是OR而非AND $criteria->addSearchCondition('name', '分類');//搜索條件,其實表明了。。where name like '%分類%' $criteria->addBetweenCondition('id', 1, 4);//between 1 and 4 $criteria->compare('id', 1); //這個方法比較特殊,他會根據你的參數自動處理成addCondition或者addInCondition, //即若是第二個參數是數組就會調用addInCondition /** * 傳遞變量 */ $criteria->addCondition("id = :id"); $criteria->params[':id']=1; /** * 一些public vars */ $criteria->select = 'id,parentid,name'; //表明了要查詢的字段,默認select='*'; $criteria->join = 'xxx'; //鏈接表 $criteria->with = 'xxx'; //調用relations $criteria->limit = 10; //取1條數據,若是小於0,則不做處理 $criteria->offset = 1; //兩條合併起來,則表示 limit 10 offset 1,或者表明了。limit 1,10 $criteria->order = 'xxx DESC,XXX ASC' ;//排序條件 $criteria->group = 'group 條件'; $criteria->having = 'having 條件 '; $criteria->distinct = FALSE; //是否惟一查詢
CommandBuildersql
$schema=Yii::app()->db->schema; $builder=$schema->commandBuilder; $command=$builder->createFindCommand($schema->getTable($tableName), $criteria);
Query Builder數組
$command = Yii::app()->getDb()->createCommand(); $command->select('name, label, controller,action')->from('{{menu_item}}'); $command->where('menu_id=:mid ', array(':pid' => $parent_id); $command->order('sort_order ASC, label ASC'); $results = $command->queryAll();
推薦compare,搜索條件容許爲空所以不須要加條件
app
$criteria = new CDbCriteria; $criteria->alias = 't'; //table t,默認select='*'; //一些public vars $criteria->select = 'id,parentid,name'; //表明了要查詢的字段,默認select='*'; $criteria->join = 'xxx'; //鏈接表 $criteria->with = 'xxx'; //調用relations $criteria->with = array('accounts','varchar'); $criteria->with = array('teacher'=> array('select'=>'username,company_id', 'order'=>'teacher.username ASC') ); $criteria->limit = 10; //取1條數據,若是小於0,則不做處理 $criteria->offset = 1; //兩條合併起來,則表示 limit 10 offset 1,或者表明了。limit 1,10 $criteria->order = 'xxx DESC,XXX ASC' ;//排序條件 $criteria->group = 'group 條件'; $criteria->having = 'having 條件 '; $criteria->distinct = FALSE; //是否惟一查詢 Article::model()->findAll($criteria);
DAOyii
$sql =' ... yiiblog_desc like :keywords or yiiblog_title like :keywords ... '; $TmpStatement = Yii::app()->db->createCommand($sql); $TmpStatement->bindValue(':keywords', '%'.$keywords.'%'); findAll('username like :username ' ,array(":username"=>"%$key%"));