Is it possible to have nested CGridViews?

If I had two tables (Post and Comment) where a post had many comments, I could easily have a page showing each post in CGridView and make clicking on one of the posts take me to page showing related comments also in grid view.

However I was wondering if there were any solutions to show the related comments shown underneath its post in some sort of nested view.

Is something like this possible? I’ve seen this post ( but I am looking to actually include CGridView nested.

try groupgridview extension

Hi Dubby,

I am new to Yii and am trying to do what you said was easy in the first part of your post. Can you please guide me how to do this? I have a department with many employees and I want to have a cgrid view of the employees when I click on a row in cgrid view of department. Thanks

Are talking about a CGridView of the departments, or of all employees? If just a department list, you can set the value of the column to a link to Employees with the selected department to view just those employees.

If a list of all Employees, then you could set up a dropdown list filter for the department column.

Thanks Rootbear,

That extensions looks good but it’s not nested as such and it only includes the one model. I think I’ll write the HTML out in a for loop to achieve what I need.

Bianca, you want something like this:

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

        array('dataProvider' => $dataProvider,




            array('name'=>'Employees', 'type'=>'raw', 'value'=>'CHtml::link("Employees", array("employees/admin", "id"=>$data["id"]))'),





and then in your Employee::actionAdmin method you could have something similar to this:

public function actionAdmin($id=null)


    $model=new Employee('search');

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

    if($id != null)







Also, rather than specifying $id=null in the function declaration you could use a filter

I just thought that a simple foreach loop will not give me the pagination that I require. I’ll have to look further into this.

Thank you very much for your prompt reply. I tried it and had to do some modifications because of the double quotes (btw is it normal? I had to replace the double quotes by single ones) Where must you define variable $dataProvider? Sorry I am new at Yii and am finding it hard to understand. Glad if you could help me. Thanks

I figured out where to put the variable $dataprovider but where do you define $data?


It’s a variable provided by $dataProvider. I didn’t understand it earlier but it seems obvious now as I type it.

Thanks Dubby. I looked for the explanation and found it but am unable to make it work: I have a problem with the syntax:

<?php $dataProvider=new CActiveDataProvider('Department'); ?>

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






		array('name'=>'Employees', 'type'=>'raw', 'value'=>'CHtml::link('Eyees', $this->createUrl('employee/admin'),array('id'=>'$data->id'))'),





)); ?>

I am having a parse error:

Parse error: parse error, expecting `’)’’ in C:\wamp\www\testdrive\protected\views\department\admin.php on line 46

I have tried to edit several times but am stuck. Line 46 refers to CHtml::link

Hi Bianca,

Your issue is that you’re encapsulating the string in single quotes but then having the variables also in single quotes. PHP is unable to determine where your string starts and stops.

Your example:

'CHtml::link('Eyees', $this->createUrl('employee/admin'),array('id'=>'$data->id'))'

try leaving the full string in single quotes but putting the variables in double quotes like this:

'CHtml::link("Eyees", $this->createUrl("employee/admin"),array("id"=>"$data->id"))'

Notice the single quotes appear as the very first and very last characters ONLY.

Further note: If you need to use single quotes (or only double quotes) throughout you need to escape them first like this:

'CHtml::link(\'Eyees\', $this->createUrl(\'employee/admin\'),array(\'id\'=>\'$data->id\'))'

So, this post has gone off topic. Can anyone help me with nested CGridView?

Sorry for being a bit late dubby.

I think this wiki is what you want: