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(




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





This goes in the _form partial of the parent Controller:

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






			'name' => 'Tipo',

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









)); ?>

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:


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.


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',


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