Accessing Attribtes From Nested Joined Table Of A Model

I have a model "MEDICATION" which has relation "GENERICS_MED_TRADENAMES". "GENERICS_MED_TRADENAMES" model contains drugForm relation referred to LIST_OF_VALUES model. After getting the result set of MEDICATION from DataProvider to show in ListView, I want to access "group_display_val" attribute of "LIST_OF_VALUES model.

Following are excerpts to show relations of my models:


'genMedStrnDrgform' => array(self::BELONGS_TO, 'GenericsMedTradenames', 'gen_med_strn_drgform_id'),


'drugForm' => array(self::BELONGS_TO, 'ListOfValues', 'drug_form_id'),

ListOfValues model has an attribute "group_display_val".

I populate the DataProvider for ListView as below:

$PatCurVisitMedicationList = new CActiveDataProvider('Medication',array('criteria'=>$criteria,'with'=>array(




In the list view, I unable to access "group_display_val" attribute of "LIST_OF_VALUES" and tried following two approaches:



I am unable to access the nested model’s attribute , any help would greatly appreciated.

Thanks in advance.


I have doubt … what is $data here

Have you tried with following

try first this






inside foreach loop like

foreach ([size=2]$data->genMedStrnDrgform->drugForm as $form[/size][size=2])[/size]

[size=2] $form->[/size][size=2]group_display_val[/size]

I am assuming your names for relations are perfect :)

"$Data" is reference to the data which I obtained from $DataProvider. $Data is used to refer to the attributes of the model against which the result set was obtained. In my case, the query was made to "Medication" which has relation "genMedStrnDrgform".

By the way, I have tried all what you suggested but all in vain.

I am still looking around but loosing hope because it seems that this is not possible. As last resort, I will write a view and query it to display what I want. Thanks for your time anyway.

Check this link

After detailed investigation, I found out that some rows did not have the referencing id because of which NULL values was coming which was resulting "$data->genMedStrnDrgform->drugForm->group_display_val" to fail. This was correct and now working.

well, if some related models are optional you have to check their presence like:

$data->genMedStrnDrgform->drugForm !== null ? $data->genMedStrnDrgform->drugForm->group_display_val : ''