Change Column In Cgridview For Another Model's Field

In my model (let’s say Model A) I have a field corresponding to the id of another model (Model B). In a CGridView composed of Model A items, instead of showing the id of Model B, I want to show another field of that model instance.

Here’s how I am showing my CGridView;


$dataProvider =  new CArrayDataProvider('Model A');

$dataProvider->setData($model_A_Data); // $model_A_data is an array of Model A Objects


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

    'id'=>'data-id',

    'dataProvider'=>$dataProvider, //$objects->search(),

    'columns'=>array(

        array('header'=>'Model A Id', 'name'=>'id'),

        array('header'=>'Name', 'name'=>'name'),

        array('header'=>'Date', 'name'=>'date_created'),

        array('header'=>'Model B Id', 'name'=>'model_b_id'), // *HERE SHOW ANOTHER FIELD OF MODEL B*

    ),  

)); 



How can I change that column in order to show another field of model B clasS?

Thanks

OK, I figured out how to solve this after discovering the $data var in CGridView which holds the corresponding row data.

Simply add this:




array('name'=>'Model B Another Field',

      'value'=> 'ModelB::model()->FindByPk($data->id)->another_field'

),



Hi

You can also look at this wiki: Sort and filter a custom or composite CGridView column.