Thursday, 6 March 2014

CActiveDataProvider Yii - Is simple and neat



CActiveDataProvider Yii - Is simple and neat, where we can enter our condition, pagination etc

I actually started coding with custom select query in YII, but while referred to "CActiveDataProvider" with conditions and pagination there is no need for to call the  sql query second time with condition or pagination

Here i will explain what i had previously and how the YII default CActiveDataProvider is working


My custom code in my controller function:
===========================================

public function actionList()

{
$criteria = new CDbCriteria();

$criteria->addCondition('user_id='.Yii::app()->user->id.'');
$count = Blog::model()->count($criteria);
$pages = new CPagination($count);

 
$pages->pageSize = 5;
$pages->applyLimit($criteria);
$models = Blog::model()->findAll($criteria);
$this->render('list', array('models' => $models, 'pages' => $pages));
}


While following the above code, i need to call this $model inside a foreach and need to show the values. 



YII default CActiveDataProvider in my controller function:
===========================================

public function actionList()
{
$criteria=new CDbCriteria;
$criteria->order='id DESC';  
$dataProvider=new CActiveDataProvider('Blog',array('criteria'=>$criteria,'pagination' => array('pageSize'=>Yii::app()->params['PaginationPerPage']),));
$this->render('list',array('dataProvider'=>$dataProvider,));
}

This is what Yii gave in default, its simple. And while following this code we can simply use the default view format from YII like below 


$this->widget('zii.widgets.CListView', array('dataProvider'=>$dataProvider,'itemView'=>'_view',)); 
?>




No comments:

Post a comment