12

Suppose I have model User which have many to many relation to itself named as friends. so $user->friends (or $model->friends in view) gives me an array of User objects. I wanted to display the friends as gridview. But CGridView data as dataProvider object. Googling for it found the way to convert array of model objects to dataProvider object as given below.

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

Now using this I get an error

Property "User.id" is not defined.

UPDATE

public function relations()
{
    return array(
         'friends' => array(self::MANY_MANY, 'User', 'friendship(user_id, friend_id)'),
    );
}
1
  • adding how you defined relation and how two models relate (PK and FK) is crucial to getting an answer! Commented Jan 3, 2013 at 10:15

2 Answers 2

18

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,
    ),
));
6
  • got the exact thing i was searching, anyways thanks dude. thumbs up to your answer too. Commented Jan 10, 2013 at 10:06
  • the exact answer that i was searching is given below. please check it and upvote if its useful Commented Jan 10, 2013 at 10:16
  • 2
    Great that I was of help :) Commented Jan 10, 2013 at 12:42
  • Please check if the below given answer is correct or not. I got the required result from that. I just want to know if its a better answer or not Commented Jan 11, 2013 at 4:13
  • its nice and efficient solution I can think of. So no need to worry! Commented Jan 11, 2013 at 8:14
13

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

1
  • 4
    this is a better answer, as it enables CGridView to fetch grid headers from the CModel...
    – Gero
    Commented Apr 10, 2013 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.