【发布时间】:2017-08-17 06:43:17
【问题描述】:
在本书的这个例子中:
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');
}
}
我可以在 students/view/N 中看到与每个学生相关的课程列表,因为我在 StudentsController 中有以下代码:
public function view($id = null)
{
$student = $this->Students->get($id, [
'contain' => ['Cources']
]);
$this->set('student', $student);
$this->set('_serialize', ['student']);
}
如果我在那里用 CoursesMemberships 替换 Courses:
public function view($id = null)
{
$student = $this->Students->get($id, [
'contain' => ['CoursesMemberships']
]);
$this->set('student', $student);
$this->set('_serialize', ['student']);
}
在Students/view.ctp中进行相应修改,可以看到相关CoursesMemberships列表。
如何查看两者?我的意思是,StudentController 中应该包含什么,以便我可以在同一个视图中(在同一张表中更好)看到每个 Student,相关的 Courses 以及 days_attended 和 Grade?p>
【问题讨论】: