0
表在这书上的例子:检索数据并加入CakePHP的3.5
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']);
}
如果我通过CoursesMemberships有替代课程:
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和grade?
谢谢!它现在真的有用。 –
我有一种方法来添加,查看,编辑和删除从这张表'CoursesMemberships'使用Cake代码生成?索引起作用,它正确地列出所有条目,其余部分最终给出'InvalidPrimaryKeyException'。 –