将附加数据保存到CakePHP 3.0中add方法中的联接表中
在本书的示例中: 我想在添加新的将附加数据保存到CakePHP 3.0中add方法中的联接表中,php,mysql,cakephp-3.0,jointable,Php,Mysql,Cakephp 3.0,Jointable,在本书的示例中: 我想在添加新的学生时输入与课程#8相对应的年级。我举两个例子: 及 并在studentscocontroller.php public function add() { $student = $this->Students->newEntity(); if ($this->request->is('post')) { $student = $this->Students->
学生
时输入与课程#8相对应的年级
。我举两个例子:
及
并在studentscocontroller.php
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');
当我在视图中选择课程#8并输入相应的等级时,我在
CoursesMemberships
表中看不到它。它会添加记录本身,但是成绩不在那里。起初,我在添加函数中没有看到$course
也许如果你把网站改成$courses
$items=$this->Students->Courses->find('list',['limit'=>200])
或者在你看来:
echo $this->Form->control('courses._ids', ['options' => $items]);
起初我在你的add函数中没有看到$course
也许如果你把网站改成$courses
$items=$this->Students->Courses->find('list',['limit'=>200])
或者在你看来:
echo $this->Form->control('courses._ids', ['options' => $items]);
您不能混合使用特殊的\u id
语法和表示嵌套数据结构的传统语法,即property.index.field…
(文档中关于该行为的注释可能不会有任何影响)
另外,在索引5
处添加数据似乎非常随意,您正在添加一个新的链接/记录,因此通常从0
开始
放弃\u id
语法,通过显式定义其主键来构建适当的课程数据集,如:
echo $this->Form->control('courses.0.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.0._joinData.grade');
另见
您不能将特殊的\u id
语法与表示嵌套数据结构的传统语法混用,即属性.索引.字段…
(文档中关于该行为的注释可能不会有什么影响)
另外,在索引5
处添加数据似乎非常随意,您正在添加一个新的链接/记录,因此通常从0
开始
放弃\u id
语法,通过显式定义其主键来构建适当的课程数据集,如:
echo $this->Form->control('courses.0.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.0._joinData.grade');
另见
不,那是个印刷错误,我编辑了这个问题。这里没有项目,只有课程。不,那是印刷错误,我编辑了问题。这里没有物品,只有课程。非常感谢!确实是缺少了什么。现在一切都很顺利,非常感谢!确实是缺少了什么。现在一切都很顺利。