I have a view displaying company details, and I added a list of orders made by this company (separate model and controller).
Everything works, however the CGridView
that is used to display the list of orders is rendered (renderPartial()
) from the company's controller, and because of that, the default controller of the CGridView
is company
instead of order
, that causes all the automatically generated URLs like for the update/delete buttons to be invalid, because they are being generated as /company/delete?id=
and they should refer to the order
controller. I went around this by manually creating the URLs for the action buttons like that:
$columns[] = array(
'class' => 'CButtonColumn',
'buttons' => array(
'delete' => array(
'label' => 'Download',
'url' => 'CController::createUrl("/order/delete", array("id"=>$data->id))',
'options' => array('class' => 'download'),
),
),
'template' => '{delete}',
);
But I don't like this approach, as this requires manual creation of every URL. I tried setting the controller
field of the CGridView
, but it's read-only.
How can I modify the default controller/model that CGridView
is working on?
I also see that CGridView
generates a hidden 'keys' div with IDs of elements, and these keys have update
URLs with incorrect controller name, so I really need to somehow change the controller CGridView
is working on, as I don't want to risk it updating entries in wrong controllers.
Edit I tried creating an instance of the controller and use that instance to create the widget:
$ctrl = Yii::app()->createController('order')[0];
$ctrl->widget('zii.widgets.grid.CGridView', $grid);
But even though the owner property (read only) of the created widget is good (OrderController
), the action URLs are still being generated with the actual path (/company/
) instead of /order/
.