在CakePHP 3.0中的add方法中将其他数据保存到联接表中 [英] Saving Additional Data to the Join Table in the add method in CakePHP 3.0

查看:55
本文介绍了在CakePHP 3.0中的add方法中将其他数据保存到联接表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这本书的示例中:

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');
    }
}

我想输入与课程#8相对应的grade,同时添加一个新的student.我遵循以下两个示例:

I want to enter grade corresponding to course #8 while adding a new student. I follow these two examples:

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

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表中看不到它.它会添加记录本身,但grade不存在.

When I select course #8 in the view and enter corresponding grade, I do not see it in CoursesMemberships table. It adds the record itself, but grade is not there.

推荐答案

您不能混合使用特殊的_ids语法和表示嵌套数据结构的传统语法,即property.index.field...(文档中有关该行为的注释不会受伤).

You cannot mix the special _ids syntax and the traditional syntax for expressing nested data structures, ie property.index.field... (a note in the docs regarding that behavior probably wouldn't hurt).

另外在索引5处添加数据似乎非常随意,您要添加新的链接/记录,因此通常从0开始.

Also adding data at index 5 seems very arbitrary, you're adding a new link/record, so you'd normally start at 0.

抛弃_ids语法,并通过显式定义其主键来构建适当的课程数据集,例如:

Ditch the _ids syntax and build a proper course dataset by explicitly defining its primary key, like:

echo $this->Form->control('courses.0.id', ['type' => 'select', 'options' => $courses]);
echo $this->Form->control('courses.0._joinData.grade');

另请参见

这篇关于在CakePHP 3.0中的add方法中将其他数据保存到联接表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆