cakephp 3多级关联保存

时间:2015-06-02 16:12:13

标签: 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'
    ]);
}

用户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() ?>

以下是发送到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']);
}

1 个答案:

答案 0 :(得分:12)

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

我已将此应用于我的控制器,现在可以使用了。它在各个级别都有节省。

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