CGridView generate urls to other controler than current path

(davidev) #1

I have two models and controllers:
orders -> ordersController
clients -> clientsController

Clients create orders, and in the clientsController admin action I render a view which displays the details about the client. I wanted to add a list of orders that this client has made in the past (status = finished).
For this, I added a getter to the clients model to get the data provider:

public function getOrdersDataProvider()
    $model = new orders('search');
	$model->client_id = $this->id;
	$model->dbcriteria->addCondition("status = 'finished'");
	return $model->search();

In the admin view of the clientsController I added renderPartial with the _orders view, passing $model->ordersDataProvider as parameter.

In the view I have a CGridView widget:

    $grid = array(
	'id' => 'orders-grid',
	'dataProvider' => $dataProvider,
	'ajaxUrl' => '/orders',

I then add the columns to display, and then the CButtonColumn with actions like delete/update etc:

  $columns[] = array(
	'class' => 'CButtonColumn',
	'headerHtmlOptions' => array('class' => 'buttonColumn1'),
	'buttons' => array(
		'update' => array(
			'url' => 'CController::createUrl("/orders/update", array("id"=>$data->id))',
		'delete' => array(
			'url' => 'CController::createUrl("/orders/delete", array("id"=>$data->id))',
	'template' =>  '{update}{delete}',

Then the widget is just added by $this->widget('zii.widgets.grid.CGridView', $grid);
As you can see, I use CController::createUrl for the action urls. That is my problem, the widget automatically generates URLS basing on the current path (/clients) and therefore the action buttons referred to the wrong controller by default, and I would delete my clients instead of the orders. I went around this by using createUrl, but I really don’t like this approach, I also see that GridView generates a hidden div with keys and update urls that are still referring to the wrong controller, and I’d like to make it the way it should be. I tried setting the controller & owner values of the grid, but they are read-only. I also tried creating an instance of the orders controller and using it to create the widget, which resulted in the owner being properly set to ordersController, but the urls were still generated incorrectly.
How can I properly (and globally, so I won’t have to createUrl everywhere) change the way CGridView generates urls?