Yii CDbCriteria Tutorials

CDbCriteria In Yii

CDbCriteria is the one of best support in yii framework. CDbCriteria is used to assign the values or property in query. Using cdbcriteria property we can assign condition, order, limit, scopes etc for query. It have some methods to apply condition for model or sql table like addCondition(), addInCondition() etc .aliasConditionDistinctGroupHavingJoinLimit, OffsetOrder byParamsscopesSELECTtogetherWithaddBetweenConditionCompareaddConditionaddInConditionaddNotInConditionaddSearchCondition Note: $model is the object of 'post' table classaliasthe alias name of the table $criteria=new CDbCriteria; $criteria->alias = […]... Read More »

CDbCriteria Search

Yii framework model have search() function. We can create more function like search. I created new function ‘searchcategory’ and i added some custom function to searchcategory() function. Example public function searchcategory() { $criteria=new CDbCriteria; $condition=''; $categoryname=$this->categoryname; if($this->categoryname1!='') { $categorystring=$this->likecategoryname($this->categoryname1); if($categorystring!='') { $condition="categoryidIN (".$categorystring.")"; } } if($categoryname!='') { $categoryidstring=$this->likecategoryname($categoryname); if($categoryidstring!='') { if($condition!='') $condition.=' AND '; $condition.="categoryid IN (".$categoryidstring.")"; } } if($condition!='') { $criteria->condition=$condition; } return new CActiveDataProvider($this, array( 'criteria'=>$criteria, )); } […]... Read More »

CDbCriteria Sample in YII

Sample 1 <?php $criteria = new CDbCriteria; $criteria->mergeWith(array( 'join'=>'LEFT JOIN user_group ug ON ug.user_id = t.id', 'condition'=>'ug.group_id = 0 OR ug.group_id IS NULL', )); User::model()->findAll($criteria); ?> Sample 2 <?php class Mymodel extends CActiveRecord { public function relations() { return array( 'mySecondTable' => array( self::HAS_MANY, 'MySecondTable', 'second_table_id' ), ); } public function searchWithRelated() { $criteria = new CDbCriteria; //without this you wont be able to search the second table's data $criteria->together […]... Read More »