在本书的例子中:
https://book.cakephp.org/3.0/en/orm/associations.html#using-the-through-option
class StudentsTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Courses', [
'through' => 'CoursesMemberships',
]);
}
}
class CoursesTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Students', [
'through' => 'CoursesMemberships',
]);
}
}
class CoursesMembershipsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Students');
$this->belongsTo('Courses');
}
}
我可以在学生/视图/ N中看到与每个学生相关的课程列表,因为我在StudentsController中有以下代码:
public function view($id = null)
{
$student = $this->Students->get($id, [
'contain' => ['Cources']
]);
$this->set('student', $student);
$this->set('_serialize', ['student']);
}
如果我按课程会员替换课程:
public function view($id = null)
{
$student = $this->Students->get($id, [
'contain' => ['CoursesMemberships']
]);
$this->set('student', $student);
$this->set('_serialize', ['student']);
}
并在Students / view.ctp中进行相应的更改,我可以看到相关课程成员列表。
如何看待两者?我的意思是,在StudentsController中应该是什么,以便我可以在同一个视图中看到(在同一个表中更好)每个学生,相关的课程和days_attended和年级?
答案 0 :(得分:0)
使用第一个变体,即包含Courses
,联接表数据将在Course
实体_joinData
属性中提供。
$course->_joinData->days_attended
$course->_joinData->grade