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:

MEDICATION


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

GenericsMedTradenames


'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(

        'genMedStrnDrgform',

        'genMedStrnDrgform.drugForm'

    )));

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


$data->genMedStrnDrgform->drugForm->group_display_val


$data->drugForm->group_display_val



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

Thanks in advance.

Hi,

I have doubt … what is $data here

Have you tried with following

try first this

$data->genMedStrnDrgform

then

$data->genMedStrnDrgform->drugForm

then

$data->genMedStrnDrgform->drugForm

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

http://www.yiiframework.com/doc/guide/1.1/en/database.arr

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 : ''