Yii Framework Forum: multiple models in gridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

multiple models in gridview Rate Topic: -----

#1 User is offline   renathy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 82
  • Joined: 05-July 11

Posted 13 August 2011 - 04:34 AM

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.
0

#2 User is offline   pommeverte 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 15-March 11

Posted 13 August 2011 - 05:52 AM

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 
                 ........


0

#3 User is offline   moylua 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 14-January 12

Posted 17 January 2012 - 06:58 AM

View Postpommeverte, on 13 August 2011 - 05:52 AM, said:


'value'=>$data->holder->name


[/code]


the code doesn't work. It gives "Trying to get property of non-object" error. Is $data the $model?
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,353
  • Joined: 12-October 09
  • Location:Croatia

Posted 17 January 2012 - 08:12 AM

$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"'

Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   moylua 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 14-January 12

Posted 17 January 2012 - 08:37 AM

View Postmdomba, on 17 January 2012 - 08:12 AM, said:

$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 :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users