在 CakePHP 3.0 的 add 方法中将附加数据保存到 Join Table
Saving Additional Data to the Join Table in the add method in CakePHP 3.0
在书中的这个例子中:
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');
}
}
我想在添加新的student
的同时输入对应课程#8的grade
。我遵循以下两个示例:
https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-belongstomany-associations
和
https://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs
并在StudentsController.php
中修改add
方法
public function add()
{
$student = $this->Students->newEntity();
if ($this->request->is('post')) {
$student = $this->Students->patchEntity($user, $this->request->getData(), [
'associated' => [
'Courses']]);
if ($this->Students->save($student,['associated' => ['Courses._joinData']])) {
$this->Flash->success(__('The student has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The student could not be saved. Please, try again.'));
}
$courses = $this->Students->Courses->find('list', ['limit' => 200]);
$this->set(compact('student', 'courses'));
$this->set('_serialize', ['student']);
}
和Students/add.ctp
有
echo $this->Form->control('courses._ids', ['options' => $courses]);
echo $this->Form->control('courses.5._joinData.grade');
当我select课程#8在视图中输入相应的成绩时,我在CoursesMemberships
table中没有看到它。它添加了记录本身,但是 grade
不存在。
起初我在你的添加函数中没有看到 $course...
也许如果您将 sitems 更改为 $courses
$items = $this->Students->Courses->find('list', ['limit' => 200]);
或者在您看来:
echo $this->Form->control('courses._ids', ['options' => $items]);
您不能混合使用特殊的 _ids
语法和表达嵌套数据结构的传统语法,即 property.index.field...
(文档中关于该行为的说明可能不会造成伤害)。
还在索引 5
处添加数据似乎非常随意,您正在添加一个新的 link/record,因此您通常会从 0
.
开始
放弃 _ids
语法并通过显式定义其主键来构建适当的课程数据集,例如:
echo $this->Form->control('courses.0.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.0._joinData.grade');
另见
在书中的这个例子中:
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');
}
}
我想在添加新的student
的同时输入对应课程#8的grade
。我遵循以下两个示例:
https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-belongstomany-associations
和
https://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs
并在StudentsController.php
add
方法
public function add()
{
$student = $this->Students->newEntity();
if ($this->request->is('post')) {
$student = $this->Students->patchEntity($user, $this->request->getData(), [
'associated' => [
'Courses']]);
if ($this->Students->save($student,['associated' => ['Courses._joinData']])) {
$this->Flash->success(__('The student has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The student could not be saved. Please, try again.'));
}
$courses = $this->Students->Courses->find('list', ['limit' => 200]);
$this->set(compact('student', 'courses'));
$this->set('_serialize', ['student']);
}
和Students/add.ctp
有
echo $this->Form->control('courses._ids', ['options' => $courses]);
echo $this->Form->control('courses.5._joinData.grade');
当我select课程#8在视图中输入相应的成绩时,我在CoursesMemberships
table中没有看到它。它添加了记录本身,但是 grade
不存在。
起初我在你的添加函数中没有看到 $course...
也许如果您将 sitems 更改为 $courses
$items = $this->Students->Courses->find('list', ['limit' => 200]);
或者在您看来:
echo $this->Form->control('courses._ids', ['options' => $items]);
您不能混合使用特殊的 _ids
语法和表达嵌套数据结构的传统语法,即 property.index.field...
(文档中关于该行为的说明可能不会造成伤害)。
还在索引 5
处添加数据似乎非常随意,您正在添加一个新的 link/record,因此您通常会从 0
.
放弃 _ids
语法并通过显式定义其主键来构建适当的课程数据集,例如:
echo $this->Form->control('courses.0.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.0._joinData.grade');
另见