need help displaying a relations field through CGridView

hi all,

I have two tables.

Projects -



Gallery -


project_id //integer =…foreign key in other words



In my admin view of the gallery I would like instead of the project_id [which is an int] to display its respective name from table Projects. In my admin view I have:

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














)); ?>

The name should be represented by the array column but I don’t know what to put against value. Can anyone help me. I know that when I call the admin view the relations query is being executed [i can see that in the logs] and it is correct so I guessing its a matter of calling the value in the correct way.

My controller has the following admin action:

public function actionAdmin()


		$dataProvider=new CActiveDataProvider('Gallery', array(












project is the relation declared as:

return array(

                    'project'=>array(self::BELONGS_TO,'Projects', 'project_id'),


Thanks for any help




Thanks Flavio it works now. What is the logic behind this. I could not see this part explained anywhere.

I learned this from CGridView.php source:

 * $this->widget('zii.widgets.grid.CGridView', array(

 *     'dataProvider'=>$dataProvider,

 *     'columns'=>array(

 *         'title',          // display the 'title' attribute

 *         '',  // display the 'name' attribute of the 'category' relation

 *         'content:html',   // display the 'content' attribute as purified HTML

 *         array(            // display 'create_time' using an expression

 *             'name'=>'create_time',

 *             'value'=>'date("M j, Y", $data->create_time)',

 *         ),

 *         array(            // display 'author.username' using an expression

 *             'name'=>'authorName',

 *             'value'=>'$data->author->username',

 *         ),

 *         array(            // display a column with "view", "update" and "delete" buttons

 *             'class'=>'CButtonColumn',

 *         ),

 *     ),

 * ));

Ok hands up. I looked at this one millions times…i guess i need some sleep. Thanks again for your help

I’m newbie here, so forgive me if question is silly… I did as it is written above however I cannot sort or search by this column (field from foreign table). How can I do this?

I have the exact same problem. Any ideas?

I’ve solved it like this:

    // CGridView column definition:







// Preparing the DataProvider in the model:

return new CActiveDataProvider(get_class($this),array(






                'desc'=>'customer.shortname desc',


I’ve made it work like this. HTH

I’ve posted examples of using CGridView with relations as well as checkboxes here: