Access Related Table Columns Within Yii

Hi,

[size="2"]I know how to access "one_plant" columns but NOT how to access "one_species" columns.[/size]

What is the cleanest way to do it from a CGridView or TbGridView stand point?

Regards

Kavi

You’re talking about grid column definition? You can use dots syntax like one_plant.one_species .

Hi NICK,

I think I’ve been not so clear in my asking.

I have a TbGridView on the table "one_plant_ event" and what i would like to be able is to access "one_species" fields from the grid considering that there is an "indirect" relationship between one_plant_event and "one_species".

So playing with the admin.php i would add lines like the following to see the fiels of one_species:

array(

'header' =>  Yii::t('app','Name In Argentine'),


'value' => 'xxxxx',

[size=“2”] [/size][size=“2”]‘filter’ => ‘xxxxx’,[/size]

), 

[size="2"]Hope is more clear[/size]

Regards

Kavi

Indirect relations are supported by Yii. You can use the syntax I mentioned earlier. You can use relation name and then refer to one of it’s attributes and Yii will fetch related objects automatically. This is done via lazy loading so if you want to prefetch everything in one query use eager loading by specifying the with property in a CDbCriteria object. Read more in docs or in the guide.

I was looking at the guide and probably I didnt get it 100% but looking at this http://www.yiiframew…e-model-fields/ i find the solution.

In my case

array(

'header' =>  Yii::t('app','Name In Argentine'),

[size=“2”] [/size][size=“2”]‘value’=>’$data->plant->species->name_in_argentine’, [/size]

),

made the solution appear.

NOw I am looking for the filter solution but first I will be checking a bit more.

Regards

Kavi

You could just declare the columns as string: ‘plant.species.name_in_argentine:’.Yii::t(‘app’,‘Name In Argentine’)