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'。 –