【问题标题】:cakephp 3 multi level associated savecakephp 3 多级关联保存
【发布时间】:2015-06-02 16:12:12
【问题描述】:

我有一个用户模型,它与员工模型具有 hasOne 关系。当我保存一个用户时,我还在Employees 表中添加了一条带有user_id 的记录。这很好用。

Employee 可以通过 CoursesEmployees 表关联到 belongsToMany Courses。当我只保存员工时,这可以保存。

我的问题是我想保存所有 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'
    ]);
}

用户添加.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方法保存的post数据。这样,用户和员工就可以正确保存,但不会记录在 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'
        ]
    ]
]
]

用户控制器.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']);
}

【问题讨论】:

    标签: cakephp cakephp-3.0


    【解决方案1】:

    我找到了解决方案。我再次阅读了食谱,并在其中一个示例中注意到了 sn-ps 'associated' =&gt; ['Tags', 'Comments.Users']

    我已经将它应用到我的控制器上,它现在可以工作了。它在各个层面都在节省。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多