Search code examples
databaseactiverecordyiiyii-components

how to add a parent table value CGridView using yii


Currently my CGridView gives following result.

http://imageshack.us/a/img821/2391/44264318.png

Here City is actually CityID From a parent table. City table has one-to-many relationship with Campus table. I want to show city.cityname instead of city.cityid from parent table. Can someone help please. Database structure is as

http://imageshack.us/photo/my-images/845/82338990.png/


Solution

  • change that column to

    array (
    'name'=>'mycol
    'value'=>'$data->city->cityname',
    );
    

    where city is a relation name, and cityname is an attribute from the linked class.

    To sort see this article http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/