cakephp 3多级关联保存

cakephp 3 multi level associated save

我有一个 Users 模型,它与 Employees 模型有 hasOne 关系。当我保存一个用户时,我还在 Employees table 和 user_id 中添加了一条记录。这很好用。

员工可以通过 CoursesEmployees table 关联到 belongsToMany 课程。当我只保存 Employees 时,这样就可以保存了。

我的问题是我想保存全部 3 个,但课程没有保存。

保存用户 -> 保存新员工 user_id -> 保存为 employee_id courses_employees

中的新员工选择的课程

EmployeesTable.php

public function initialize(array $config)
{
    $this->table('employees');
    $this->displayField('name');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');
    $this->belongsTo('Users', [
        'foreignKey' => 'user_id',

    ]);
    $this->belongsTo('Hotels', [
        'foreignKey' => 'hotel_id',
        'joinType' => 'INNER'
    ]);
    $this->belongsToMany('Courses', [
        'foreignKey' => 'employee_id',
        'targetForeignKey' => 'course_id',
        'joinTable' => 'courses_employees'
    ]);
}

用户add.ctp:

<?= $this->Form->create($user) ?>
<fieldset>
    <legend><?= __('Add User') ?></legend>
    <?php
        echo $this->Form->input('role_id', ['options' => $roles, 'empty' => true]);
        echo $this->Form->input('email');
        echo $this->Form->input('active');
        echo $this->Form->input('activation_key');
        echo $this->Form->input('password');
        echo $this->Form->input('employee.name');
        echo $this->Form->input('employee.email');
        echo $this->Form->input('employee.surname');
        echo $this->Form->input('employee.employee_num');
        echo $this->Form->input('employee.courses._ids', ['options' => $courses]);
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

这里是post发送到add方法保存的数据。有了这个,用户和员工得到正确保存,但没有记录在 courses_employees.

如果我从员工控制器中保存员工,courses_employees 可以添加,所以我知道关联设置正确。

[
'role_id' => '1',
'email' => 'asdasd@asdasd.com',
'active' => '11111111111',
'activation_key' => '1',
'password' => 'asdasdadasdasdasda',
'employee' => [
    'name' => 'asdasdasdasd',
    'email' => 'asdasdasd2dasd.com',
    'surname' => 'asdasdads',
    'employee_num' => 'asdasdadasdas',
    'courses' => [
        '_ids' => [
            (int) 0 => '2'
        ]
    ]
]
]

UserController.php

public function add()
{
    $this->loadModel('Courses');
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        $user = $this->Users->patchEntity($user, $this->request->data, [
            'associated' => ['Employees', 'Courses']
        ]);
        if ($this->Users->save($user)) {
            $this->Flash->success(__('The user has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
    }
    $roles = $this->Users->Roles->find('list', ['limit' => 200]);
    $courses = $this->Courses->find('list', ['limit' => 200]);
    $this->set(compact('user', 'roles', 'courses'));
    $this->set('_serialize', ['user']);
}

我找到了解决办法。我再次阅读了这本食谱,并在其中一个示例片段中注明 'associated' => ['Tags', 'Comments.Users']

我已将此应用到我的控制器,现在可以使用了。它在所有层面上都在节省。

$user = $this->Users->patchEntity($user, $this->request->data, [
    'associated' => ['Employees', 'Employees.Courses']
]);