Find us on facebook

Jul 29, 2014

Relationships in Yii

BELONGS_TO: e.g. Student belongs to School
HAS_MANY: e.g. School has many Student
HAS_ONE: e.g. Teacher has at most one School
MANY_MANY: e.g. Student belongs to many Teacher and Teacher has many Student.

​​class Student extends CActiveRecord
{
  public function relations()
     {
  return array(
              'school'=>array(self::BELONGS_TO, 'School', 'school_id'),
              'teachers'=>array(self::MANY_MANY, 'Teacher','tbl_student_teacher(student_id, teacher_id)'),
         );
 }
}

class School extends CActiveRecord
{
    ......
    public function relations()
    {
        return array(
            'students'=>array(self::HAS_MANY, 'Student', 'school_id'),
            'teacher'=>array(self::HAS_ONE, 'Teacher', 'school_id'),
        );
    }
}

No comments:

Post a Comment