Using CGridView for relations (embedded in parent view)

Hi!

I would like to display the relations of a certain object in a grid view. Thus I edited the view.php of my class ‘Person’ (instance is $model) as follows:




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

		  'dataProvider' => new CArrayDataProvider($model->addresses, array()),

		  'columns'=>array(		    

			'street',

			'town',

			'post_code',

			array(

				'class' => 'CButtonColumn',			

		        ), 

		));	



Issues:

  1. The links generated by CButtonColum point to "person/create" instead of "address/create"

  2. Headers of the grid are not translated.

The same grid works fine when it used with the "$model->search()" DataProvider in the according view "address/admin.php".

I can fix issue number 1 by overriding the urls by:




..

'viewButtonUrl'=>'Yii::app()->createUrl("/address/view", array("id" => $data[\'id\']))',

'deleteButtonUrl'=>'Yii::app()->createUrl("/address/delete", array("id" => $data[\'id\']))',

'updateButtonUrl'=>'Yii::app()->createUrl("/address/update", array("id" => $data[\'id\']))',

..



And I can fix issue number 2 by setting the "header" property myself.

I think these fixes are just workarounds and not a clean solution. Any ideas how to use CGridView in this scenario without hacks??? Somehow CGridView refers to the outside $model instead of the models provided by CArrayDataProvider…

Best Regards

Sebastian

What is your controller called? Is it PersonController? because from CButtonColumn.php:




public $viewButtonUrl='Yii::app()->controller->createUrl("view",array("id"=>$data->primaryKey))';

public $updateButtonUrl='Yii::app()->controller->createUrl("update",array("id"=>$data->primaryKey))';

public $deleteButtonUrl='Yii::app()->controller->createUrl("delete",array("id"=>$data->primaryKey))';



So the best way is to do your first fix. For the second fix:

Go to your Address Model and change the attributes from there. change the array in attributeLabels method.

Hi Gasmin,

thanks for your reply. "attributeLabels()" is filled correctly on "Address". When I use the CGridView during the AddressController life cycle the values are translated fine. Thus I guess something breaks when it used during the PersonController life cycle.

Best Regards

Sebastian

In CDataColumn you will find:




	protected function renderHeaderCellContent()

	{

		if($this->grid->enableSorting && $this->sortable && $this->name!==null)

			echo $this->grid->dataProvider->getSort()->link($this->name,$this->header);

		else if($this->name!==null && $this->header===null)

		{

			if($this->grid->dataProvider instanceof CActiveDataProvider)

				echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));

			else

				echo CHtml::encode($this->name);

		}

		else

			parent::renderHeaderCellContent();

	}



As CArrayDataProvider is NOT an instance of CActiveDataProvider the attribute labels are not loaded.

Is there a solution to attach relations with any instance CActiveDataProvider to the CGridView?

The same was asked (and solved) here: http://www.yiiframework.com/forum/index.php?/topic/19913-cgridview-with-multiple-models.

Check his solution. But you will not want to copy his code as is, since he is using giix.

It seems the problem discussed there is different from mine.

I am in the Person view and would like to display a CGridView for "person->adresses".

In my scenario the above problem would discuss "I am in the Adress view and would like to display something from Person in the Address CGridView."