我有这张表:
Careeer { id(pk) , name}
Student { email (pk), ...}
Career_Student { student_email(pk)(fk), career_id (pk)(fk), semester }
在我的Yii模型中,我有这种关系
'careers' => array(self::MANY_MANY, 'Career', 'Career_Student(student_email, career_id)')
最后我有了这个查询
$criteria = new CDbCriteria();
$params = array (':email' => $_SESSION['USER']);
$criteria->addCondition("email=:email");
$criteria->params = $params;
$result = Student::model()->find($criteria);
$careers=$result->careers;
我的问题是Yii没有检索我的$ careers变量中的“semester”列。
¿我该怎么做?
提前谢谢!
答案 0 :(得分:0)
您可以将MANY_MANY
关系表示为与through
选项相关联的两个HAS_MANY
关系的组合:
'career_student' => array(self::HAS_MANY,'Carrer_Student','student_email'),
'careers' => array(self::HAS_MANY,'Career', 'career_id','through'=>'career_student'),
还要确保在career
模型中为Career_Student
定义关系:
'career' => array(self::BELONGS_TO, 'Career', 'career_id'),
现在,当您查询学生with('careers')
时,将填充两个关系:
$criteria->with = 'carreers';
$result = Student::model()->findAll($criteria);
foreach($student->career_student as $careerStudent) {
// You have access to the bridge table ...
echo $careerStudent->semester;
// ... and the connected career table
echo $careerStudent->career->name;
}