php - Retrieve the child model of a record in Yii Framework -
i trying understand how model child class of cactiverecord model.
i have following 2 basic classes:
- class
userextendingcactiverecord - class
tutorextendinguser
the connection between them id of table users being fk in table tutors. in current app instanciating user class, need data tutor.
the relations set gii follows:
for user class: public function relations() { return array( 'tutor' => array(self::has_many, 'tutors', 'user_id'), ); } tutor class: public function relations() { return array( 'user' => array(self::belongs_to, 'users', 'user_id'), ); } how can can object populated data both models?
note: database tables named @ plural, while models @ singular.
when query active record model should bring in related objects automatically (provided have set relationships properly). following give start.
$users=user::model()->findall(); depending on doing data need decide if wish lazily fetch related tables or eagerly fetch them. following eagerly fetch records.
$users=user::model()->with('tutor')->findall(); the yii documentation on subject , worth read: http://www.yiiframework.com/doc/guide/1.1/en/database.arr#performing-relational-query
Comments
Post a Comment