Link into 2nd admin gridview with results restricted by foreign key

Hello all – I apologize if this has been covered before but I’ve googled and searched the forums back and forth and can’t find an example. I’m trying to create a link from an admin.php CGridView into an another admin gridview but have the target CGridView only return rows that match the id (foreign key) of the row selected in the first CGridView.

A simplified example could be Customers and Servers. Customers have many servers but servers only have one customer. I’ve created FK relations with InnoDB tables and generated CRUD pages using Gii. Server contains the customer_id as a FK. This has created proper relations in my models but I can seem to find the best practice for creating a link to the Servers admin page from the customers admin page that results in the CGridView on the server admin page only returning servers for that customer id.

In Server model:

'customer' => array(self::BELONGS_TO, 'Customer', 'customer_id'),

In Customer model:

'servers' => array(self::HAS_MANY, 'Server', 'customer_id'),

If anyone can point be to an example of the best practice for this I’d greatly appreciate it!

Thanks in advance!

Do you mean that in one admin page, you can click on a link that will bring you to a 2nd page for records on that link? If so, you could just do something like this in your 1st gridview:

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








			'value'=>'CHtml::link($data["ID"], CHtml::normalizeUrl(array("Controller/2ndAdmin", "relatedID"=>$data["ID"])))', 






)); ?>

and then in the 2nd view controller:

public function actionSuperFun2ndPageWOW($relatedID)


     $model = new OtherModel;

     $model->relatedAttribute = $relatedID;

     //more stuff


this is all in the case your second CGridView is using the dataProvider of $model->search().

Thanks for the quick reply!! This is where I’ve been having the problem - I’ve formed the link to the server admin page but once there I have trouble getting actionAdmin to only return records that match the foreign key. Using your suggestion I chaged the actionAdmin in ServerController.php to:

	public function actionAdmin($relatedID=null)



		//$model=new Server('search');

	    $model=new Server;

		$model->relatedAttribute = $relatedID;


		$model->unsetAttributes();  // clear any default values








but this tosses the exception Property “Server.relatedAttribute” is not defined. It’s worth noting that both $model=new Server(‘search’); and $model=new Server; return “Server.relatedAttribute” is not defined. The model is generated by Gii so perhaps it’s missing something?

You would use the actual related attribute name, that was just an example. So I guess from your example it would be customer_id, like this:

public function actionAdmin($customer_id=null)



                $model=new Server('search');


                $model->unsetAttributes();  // clear any default values


                $model->customer_id = $customer_id;








Silly me - Thanks so much. This was exactly was I was looking for. Now on to the bookmarks!

Thanks again!

Hi Guys,

for me it’s not working, what am I missing?

this is what I see:

<a href="/index.php?r=category/admin&amp;id=1">1</a>

thanks a lot!

From first look the issue is the URL Manager is not functioning…

Probable causes:

  • you will have to rewrite it by setting urlFormat property

follow these links…

Hi yiiplayboy,

many thanks for the quick answer!

unfortunately I’m a beginner yet, so I don’t really understand what you mean exactly.

Can you please explain it a little bit more in detail?

Thanks a lot!