multiple models in gridview

I have looked through differnet topics, but I haven’t found what i need.

So, i Have:




  $dataProvider=new CActiveDataProvider('PolicyModel');


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

    'dataProvider'=>$dataProvider,

      'columns'=>array(          

                 ,'start_date'

		  ,'end_date'

	...



Howerver, now I need to add also model wich belgons to PolicyModel:




'holder'=>array(self::BELONGS_TO, 'ClientModel', 'holder_id'),



I need to display holder.name in my gridview. How to do this? I do not need any filters here. Simply display all Policy data + Holder names for policies.

Maybe this is what you’re looking for?


$dataProvider=new CActiveDataProvider('PolicyModel');


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

    'dataProvider'=>$dataProvider,

      'columns'=>array(          

                 'start_date',

                 'end_date',

  		 array('name'=>'Holder', //display name of column

                       'value'=>$data->holder->name), // value of the row for this column 

                 ........




the code doesn’t work. It gives “Trying to get property of non-object” error. Is $data the $model?

$data is the model… $data->holder is the relation… but only if there is a value for the FK…

And this should be enclosed with parenthesis.

You get the error because there is no related record so $data->holder is in your case null, so you need to test this…

something like


'value'=>'$data->holder !== null ? $data->holder->name : "n/a"'

Thanks it works now :)