1

I have a table "MEDICATION" which is joined with "GENERICS_MED_TRADENAMES" table. "GENERICS_MED_TRADENAMES" table contains drug form of medicine which is referred in LIST_OF_VALUES table. After getting the result set of MEDICATION from DataProvider to show in ListView, I want to access DRUG_FORM description stored in LIST_OF_VALUES table but using Yii Model relations. I tried following but it did not work:

'genMedStrnDrgform' => array(self::BELONGS_TO, 'GenericsMedTradenames', 'gen_med_strn_drgform_id'), -- This is okay, I can access the attributes 
'drugForm' => array(self::BELONGS_TO, 'ListOfValues', 'genMedStrnDrgform.drug_form_id'), -- This does not work since it is nested. Yii thorws error.

To further explain what I want, I have also explained the same in form of SQL queries (Putting a snapshot because indentation is very difficult to present my question in a proper way):

enter image description here

Edit after Pentim10 reply:

I need to display DrugFrom description and tried the following in ListView file but both are not working: drugForm->group_display_val

"$data->genMedStrnDrgform->drugForm->group_display_val"
 "$data->drugForm->group_display_val" 

1 Answer 1

1

Place in Medication:

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

Place in GenericsMedTradenames

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

then do the query like this:

$posts=Medication::model()->findAll(array(
    'with'=>array(
        'genMedStrnDrgform',
        'genMedStrnDrgform.drugForm'
    )
));
5
  • How to access "drugForm->group_display_val" in ListView file. I have tried this "$data->genMedStrnDrgform->drugForm->group_display_val" and also ""$data->drugForm->group_display_val" but both of these do not work. This is the actual problem from where I started. Thanks for your support anyway.
    – FaisalKhan
    Feb 1, 2014 at 21:34
  • that's another question, but if you've done with "with" and "together" than it will be just there. You also need to make sure that each relation is setup correctly. Update your question with new details if you reached another level where you are blocked.
    – Pentium10
    Feb 1, 2014 at 21:35
  • It's great that the two of you understand each other - I HAVE NO IDEA
    – Strawberry
    Feb 1, 2014 at 23:33
  • @Pentium10 I have updated the original question. I was wondering if you could share some thoughts on getting the "$data->genMedStrnDrgform->drugForm->group_display_val". thanks.
    – FaisalKhan
    Feb 6, 2014 at 18:35
  • 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.
    – FaisalKhan
    Feb 7, 2014 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.