1
保存表單數據試圖建立關於如何使用加入額外的數據表一個例子,我有以下設置:
CakePHP的3,對關聯表
- 表
students
:id
,name
,[.. ] - 表
courses
:id
,title
,[...] - 連接表
courses_students
:id
,course_id
,student_id
,grade
,hours_attended
兩個基表的:
class StudentsTable extends Table {
public function initialize(array $config) {
$this->belongsToMany('Courses', [
'alias' => 'Courses',
'foreignKey' => 'student_id',
'targetForeignKey' => 'course_id',
'joinTable' => 'courses_students',
'through' => 'CoursesStudents',
]);
}
class CoursesTable extends Table {
public function initialize(array $config) {
$this->belongsToMany('Students', [
'alias' => 'Students',
'foreignKey' => 'course_id',
'targetForeignKey' => 'student_id',
'joinTable' => 'courses_students',
'through' => 'CoursesStudents',
]);
}
而且關聯表:
class CoursesStudentsTable extends Table {
public function initialize(array $config) {
$this->belongsTo('Courses', [
'alias' => 'Courses',
'foreignKey' => 'course_id'
]);
$this->belongsTo('Students', [
'alias' => 'Students',
'foreignKey' => 'student_id'
]);
}
具有表中提供了一些課程,我嘗試添加和編輯學生記錄。設置
[courses] => [_ids]
在學生記錄中創建學生表和關聯表中的記錄。
如何保存學生記錄時,應該如何形成發佈數據數組,以便能夠將grade
和hours_attended
字段存儲在關聯表中?