In this example, we have a Cars table, as well as a Dealers table. A dealer has many cars.
I want to show the (single) Dealer view, but also all of the cars that belong to this dealer in a grid on the same page. Here’s how:
In your Dealer model, you should have something like this:
public function relations()
{
return array(‘cars’ => array(self::HAS_MANY, ‘Cars’, ‘dealer_id’),);
}
In your view, you have to convert the related cars data to a CArrayDataProvider for it to work with the CGridview. You also need to adjust the button Urls in the CGridView.
In your Dealer view "view.php", below your CDetailView, add this:
$config = array();
$dataProvider = new CArrayDataProvider($rawData=$model->cars, $config);
$this->widget(‘zii.widgets.grid.CGridView’, array(
'dataProvider'=>$dataProvider
, 'columns'=>array(
'id'
, 'name'
, array(
'class'=>'CButtonColumn'
, 'viewButtonUrl'=>'Yii::app()->createUrl("/Cars/view", array("id"=>$data["id"]))'
, 'updateButtonUrl'=>'Yii::app()->createUrl("/Cars/update", array("id"=>$data["id"]))'
, 'deleteButtonUrl'=>'Yii::app()->createUrl("/Cars/delete", array("id"=>$data["id"]))'
)
)
));
Note that the viewButtonUrl is a PHP expression, but it’s quoted. The $data field is the name of the row object inside the grid.