CGridView of a model inside the view of another model

Hello everyone.

I want to display and manage the items that belong to a different model.

In my controller I have this code to create a dataProvider in the parent Controller:




$vehiculo_modelos_dp = new CActiveDataProvider('vehiculo_modelo', array(

	'criteria'=>array(

		'condition'=>'vehiculo_marca_id=:vehiculo_marca_id',

		'params'=>array(':vehiculo_marca_id'=>$model->id),

		'order'=>'t.orden, vehiculo_modelo asc',

		'with'=>'vehiculo_tipo',

		'together'=>true,

	),

));



This goes in the _form partial of the parent Controller:




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

	'dataProvider'=>$vehiculo_modelos_dp,

	'columns'=>array(

		'id',

		'vehiculo_modelo',

		array(

			'name' => 'Tipo',

			'value' => '$data->vehiculo_tipo->vehiculo_tipo',

			'sortable'=>true,

		),

		'orden',

		'activo',

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



The grid shows up alright, but I can’t sort it (nothing happens when I click the header) and the links of the action buttons (delete, edit, view) act on the parent model, not the child.

Also, my column named "Tipo" is not sortable (there is no link to click).

Can you help me work this out please?

try using ‘name’ => ‘vehiculo_tipo’, and it should be sortable now

@ bettor:

Using ‘name’=>‘vehiculo_tipo_id’ (which is the name of the field) makes it sortable, thank you!

However, it doesn’t sort. I click any header and the order stays the same.

The link on the header is:




http://transistor.local/vmc_yii/admin.php?r=vehiculo_marca/update&id=1&vehiculo_modelo_sort=vehiculo_tipo_id&ajax=yw0



The problem is that the link points to vehiculo_marca (because it is in the view of that controller, which is the parent), but it should point to the vehiculo_modelo (which is the child).

Maybe I need a way to pass the vehiculo_modelo_sort to the CGridView thru the view… I’ll try this.

If anyone else knows what to do to make my CGridView sort and delete, edit, view, please let me know.

Thanks

If you use ‘order’ in the dataprovider then the grid is not sortable by other columns because the SQL command has the “order by …”

So instead of




'order'=>'t.orden, vehiculo_modelo asc',



use




'sort'=>array('defaultOrder'=>'t.orden, vehiculo_modelo asc',),