Unable To Render Many-To-Many Data In Grid.* Widget

I have playlists and medias with many-to-many relationship (a play list can contain any no. of medias and a media can belong to any no. of playlists). I need to show all the playlists of a user along with the medias associated with it.


<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>

and in the _view, I have


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

    'dataProvider'=>new CArrayDataProvider($data->medias),

));?>

though media is array, it doesn’t render it properly (I mean, it shows summary, ie.e 1 of 2, but no data). For now, I’m using custom foreach to render data. Am I missing anything obvious?

for carraydataprovider you must provide which attribute holds id (keyField) for objects in array. read: http://www.yiiframework.com/wiki/378/an-important-tip-when-you-are-using-carraydataprovider/

Thank you, but in my case, key field is named as id only. Even then, I tried specifying key field, but in vain. First of all, can use a dataProvider in the view specified by another dataProvider?

Why not. it should not be the issue. try to debug what is exactly passed (start with empty rendering view (itemView) with just some static text)

Dear seng

I doubt that you have not included the columns section in CGridView.

Also try to asssign a unique id for each grid.




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

	'id'=>$data->id,

	'dataProvider'=>new CArrayDataProvider($data->medias,array(

	'sort'=>array('attributes'=>array('id','someAttribute'))

	)),

	//'filter'=>$model,

	'columns'=>array(	

		'id',

		'someAttribute',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



Hi,

I debugged the data, still couldn’t trace the issue. The following code works and renders the data correctly


<?php foreach($data->medias as $media):?>

			<h4><?php echo $media->title; ?></h4>

	<?php endforeach;?> 

But this one didn’t


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

		'dataProvider'=> new CArrayDataProvider($data->medias, array(

			'id'=>'album',

			'sort'=>array(

				'attributes'=>array(

					 'id', 'title',

				),

			),

			'pagination'=>array(

				'pageSize'=>2,

			))),

		)); ?>  

Interestingly, I get the pagination when the rows are over 2, get summary, just not the data. I also would like to tell that medias is an objects array and as per CArrayDataProvider we can also provide such data.

@Seenivasan: The parent widget is CListView, not gridview.

Dear Friend

By looking at the following code, one can conclude that you have not still declared that property columns.




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

                'dataProvider'=> new CArrayDataProvider($data->medias, array(

                        'id'=>'album',

                        'sort'=>array(

                                'attributes'=>array(

                                         'id', 'title',

                                ),

                        ),

                        'pagination'=>array(

                                'pageSize'=>2,

                        ))),

                )); ?>  



The following is to just simulate your scenario.

I have a Model Author.

I have a Model Book.

They have MANY_MANY relation.

views/author/index.php




<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view',

)); ?>



views/author/_view.php




<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->id), array('view', 'id'=>$data->id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('name')); ?>:</b>

	<?php echo CHtml::encode($data->name); ?>

	<br />

</div>


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

	'id'=>$data->id,

	'dataProvider'=>new CArrayDataProvider($data->books,array(

	'sort'=>array('attributes'=>array('id','name'))

	)),

	//'filter'=>$model,

	'columns'=>array(	

		'id',

		'name',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



Now every list item is containing author name followed by a grid containing books wriiten by him.

If I comment out the columns section in the following way, I am getting the same problem you are facing.




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

	'id'=>$data->id,

	'dataProvider'=>new CArrayDataProvider($data->books,array(

	'sort'=>array('attributes'=>array('id','name'))

	)),

	//'filter'=>$model,

	/*'columns'=>array(	

		'id',

		'name',

		array(

			'class'=>'CButtonColumn',

		),

	),*/

)); ?>



Regards.

Seeni, I got it working, thanks. It’s interesting that a Doctor helped an Engineer to solve an issue:)