Category Archives: Yii Cdbcriteria

CActiveDataProvider With Custom Query

Hi i used CActiveDataProvider everytime to retrieve the data for CGridView.But one of my project needed custom query in criteria and have to show the result in CGridView. During that time i found MyActiveDataProvider concept with extends of CActiveDataProvider. I applied that concept and working fine for me. I added that source code for you. I think […]

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 . alias Condition Distinct Group […]

CDbCriteria Sample in YII

Sample <?php $criteria = new CDbCriteria; $criteria->mergeWith(array( ‘join’=>’LEFT JOIN user_group ug ON ug.user_id =’, ‘condition’=>’ug.group_id = 0 OR ug.group_id IS NULL’, )); User::model()->findAll($criteria); ?> Sample <?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 […]

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.”)”; } } […]

Yii Pagination For CActiveDataProvider

This tutorial will helpful to understand about pagesize in cgridview or listview. Here i added the source code to show number of records per page. $criteria=new CDbCriteria; if($isdesired!=”){ $criteria->addCondition(“status=1”); $pagination=array(‘pageSize’=>8); $dataProvider=new CActiveDataProvider(‘Document’, array(‘criteria’=>$criteria,’pagination’=>$pagination )); $this->render(‘documentviewindex’, array(‘dataProvider’=>$dataProvider, )); }