cakephp 3多级相关保存

Kei*_*wer 4 cakephp cakephp-3.0

我有一个Users模型,它与Employees模型有一个hasOne关系.当我保存用户时,我还使用user_id在Employees表中添加记录.这很好用.

Employee可以通过CoursesEmployees表与belongsToMany课程相关联.当我只保存员工时,这可以保存.

我的问题是我要保存全部3个,但课程没有得到保存.

保存用户 - >使用新的user_id保存员工 - >在courses_employees中使用employee_id保存为新员工选择的课程

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'
    ]);
}
Run Code Online (Sandbox Code Playgroud)

用户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() ?>
Run Code Online (Sandbox Code Playgroud)

以下是发送到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'
        ]
    ]
]
]
Run Code Online (Sandbox Code Playgroud)

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']);
}
Run Code Online (Sandbox Code Playgroud)

Kei*_*wer 12

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

我已将其应用于我的控制器,现在可以使用了.它在所有级别都节省了.

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