How to convert model data objects array to dataProvider

旧街凉风 提交于 2019-12-02 19:31:31

I use two stage building the provider shown below. But I found that it gives you trouble in terms of Pagination. I have not bothered to resolve that problem since am doing other things

$dataProvider =  new CArrayDataProvider('User');
$dataProvider->setData($model->friends);
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'gridUser',
    'dataProvider' =>$dataProvider,
));

That being said, your code should work (see the example below from API docs). I suspect there is wrong attribute in your relations than the provided code. Re-check the relation definition if it is ok

From Yii docs:

$rawData=Yii::app()->db->createCommand('SELECT * FROM tbl_user')->queryAll();
// or using: $rawData=User::model()->findAll(); <--this better represents your question
$dataProvider=new CArrayDataProvider($rawData, array(
    'id'=>'user',
    'sort'=>array(
        'attributes'=>array(
             'id', 'username', 'email',
        ),
    ),
    'pagination'=>array(
        'pageSize'=>10,
    ),
));

Got it :) , i can use CActiveDataProvider instead of CArrayDataProvider as given below

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'gridUser',
    'dataProvider' => new CActiveDataProvider('User', array(
            'data'=>$model->friends,
    )),
    //...... columns display list.....
));

Anyways thanks for the reply @Stefano

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!