Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii CGrid pagination and sorting with CArrayDataProvider doesnt work

I built a custom function in my model and return the raw data:

function(){
...
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
$rows=$command->queryAll();
return $rows;
}

$campModel = $model->function..

I then use those rows in CArrayDataProvider:

$dataProvider=new CArrayDataProvider($campModel);

Finally i'm trying to view using CGrid:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bo-campaigns-grid',
'dataProvider'=>$campModel,...

I'm guessing this has to do with the way CGrid is paging...but i'm lost Thanks for the help :)

like image 520
Danny Valariola Avatar asked Jun 08 '12 06:06

Danny Valariola


1 Answers

Create a new CSort and CPagination objects and assign them to your dataprovider, because CArrayDataProvider doesn't have them defined. Here is an example of CSort creation:

$dataProvider=new CArrayDataProvider($campModel);
$sort = new CSort();
$sort->attributes = array(
            'fecha'=>array(
                'asc'=>'dateA DESC',
                'desc'=>'dateA ASC',
            ),
);
$sort->route = 'myController/myMethod';
$dataProvider->sort = $sort;
$dataProvider->sort->defaultOrder='dateA DESC';
like image 51
sucotronic Avatar answered Sep 19 '22 23:09

sucotronic