Instance Gridview Widget With Model Relation Data

I have a model that results me an array of objects and when i loop trough i want to make several instances of widget gridview and pass along an array of object that comes from a relation, however the gridview is expecting a dataprovider and i’m not sure how to convert my data.

I have searched arround and someone else had the same issue but it was not solved:

Anyone an idea what the best approach is to solve this issue?

Hi zwobbel,

Did you try CArrayDataProvider? If you did, what was wrong with it? Could you explain your data and your needs a little more, probably with a pseudo code?

What i tried to do in pseudo code (this is a lite version of what i do) is for example:




<?php

//Controller

$model = Tickets::model()->with('changes')->findByPk(4);


//view

foreach($model as $model) {

print $model->title;

//Show the gridview 

  $this->widget('zii.widgets.grid.CGridView', array(                                                 

  'dataProvider' => $model->changes,                                                             

  'columns'=>array(                                                                                                                                                                            

    'name',                                                                                                                                                                                  

  ),                                                                                                 

));

}

?>



However, i know that the relation ‘changes’ will return an array objects and so its not a dataProvider.

I also tried to use the solution of stack overflow, however it did not worked it returned a php error that told me that there was no object or value or something like that…

Something like this won’t work?




<?php

//Controller

$models = Tickets::model()->with('changes')->findAllByAttributes(...);


//view

foreach($models as $model) {

  print $model->title;

  //Show the gridview

  $dataProvider = new CArrayDataProvider($model->changes);

  $this->widget('zii.widgets.grid.CGridView', array(                                                 

    'dataProvider' => $dataProvider,

    'columns'=>array(                                                                                                                                                                            

      'name',                                                                                                                                                                                  

    ),                                                                                                 

  ));

}