あなたのMcourseテーブルが呼び出されcourses、そのテーブルのモデルが呼び出されCourses、あなたのLcourseテーブルが呼び出されcourses_colleges、あなたの大学のテーブルがcollegesであり、そのテーブルのモデルがColleges
Coursesこれで、関係のあるモデルが必要になります。
public function relations() {
return array(
'colleges' => array(self::MANY_MANY, 'Colleges', 'courses_colleges(course_id, college_id)')
);
}
モデルCollegesには同様の関係が必要です。
public function relations() {
return array(
'courses' => array(self::MANY_MANY, 'Courses', 'courses_colleges(college_id, course_id)')
);
}
ここで、特定の大学で利用可能なすべてのコースを含むドロップダウンを印刷する場合。コントローラのアクションメソッドで、コースを含むその大学のモデルを取得します。
public function actionShow() {
$id = 1; // We set just some sample id. You could get it from request ofc.
$college = Colleges::model()->with('courses')->findByPk($id);
$this->render('show', array('college'=>$college));
}
今あなたの見解でこれを印刷してください:
echo CHtml::dropDownList('courses', '', CHtml::listData($college->courses, 'id', 'name'));
モデルの列はどこ'id'にありますか。'name'Courses
そんな感じ。