Frage

i'm new to Yii.

I have 3 tables.

Entry, Study and Program

Relation is: A Study has many Entries and a Study belongs to a single Program.

I wish to define a model for Entry to display a databrowser (CGridView) containing all entries, and displaying the Program that entry belongs to.

  • Entry has study_id -> referring to Study,

  • Study has program_id -> referring to a Program,

  • Program has attribute name.

I wish to display the program name where the Entry belongs to. I just know how to define a single relation Entry, but i'm totally clueless with my present problem. What should be my relation for this? and what attribute/s?

public function relations(){
    return array(
    ...
    'study' => array(self::BELONGS_TO, 'Study', 'study_id'),
    );

}

And also, if i have this relation, how will i define my sort then for the dataprovider?

'sort'=>array(
    'attributes'=>array(
        '*'
    )
)

Many thanks!

War es hilfreich?

Lösung

You define these two relations in your model class.

'study' => array(self::HAS_MANY, 'Entry','study_id'),
'program'=>array(self::BELONGS_TO,'Program',array('program_id'=>'id'),'through'=>'study'),

Now in your view file, you write var_dump($data->program) It will show list of programs of your specific entry.

You can also get help from my this link.

How to get data from Relations with joins in yii

Hope it will help you.

Thanks

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top