Saving Additional Data to the Join Table in the add method in CakePHP 3.0

拟墨画扇 提交于 2019-12-25 00:14:14

问题


In this example from the book:

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

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

and

https://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs

and modify add method in 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']);
    }

and Students/add.ctp to have

echo $this->Form->control('courses._ids', ['options' => $courses]);
echo $this->Form->control('courses.5._joinData.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.


回答1:


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).

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

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

See also

  • Cookbook > Database Access & ORM > Saving Data > Saving Additional Data to the Join Table
  • Cookbook > Views > Helpers > Form > Creating Inputs for Associated Data



回答2:


At first I don't see $course in your add function...

Maybe if you change sitems to $courses

$items = $this->Students->Courses->find('list', ['limit' => 200]);

or in your view:

echo $this->Form->control('courses._ids', ['options' => $items]);


来源:https://stackoverflow.com/questions/45905543/saving-additional-data-to-the-join-table-in-the-add-method-in-cakephp-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!