【问题标题】:Cakephp 3 - save data to belongstomany tableCakephp 3 - 将数据保存到belongstomany表
【发布时间】:2018-08-10 11:20:26
【问题描述】:

Cakephp3,我有以下表格:

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  `created` datetime NOT NULL,
  `modified` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `roles` (
  `id` int(11) NOT NULL,
  `title` varchar(45) DEFAULT NULL,
  `created` datetime NOT NULL,
  `modified` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `roles_users` (
  `id` int(11) NOT NULL,
  `note` text,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  `role_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我的意图是在用户添加和编辑视图(以及相关的控制器操作)中编辑表“roles_users”中的可用字段。我考虑过https://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs,我的用户视图(编辑)看起来像:

<div class="users form large-9 medium-8 columns content">
    <?= $this->Form->create($user) ?>
    <fieldset>
        <legend><?= __('Edit User') ?></legend>
        <?php
            echo $this->Form->control('name');

            echo $this->Form->control('roles.0._joinData.role_id', ['type'=>'select', 'options' => $roles]);
            echo $this->Form->control('roles.1._joinData.role_id', ['type'=>'select', 'options' => $roles]);
            echo $this->Form->control('roles.2._joinData.role_id', ['type'=>'select', 'options' => $roles]);

        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

底层控制器动作是:

public function edit($id = null)
    {

        $user = $this->Users->get($id, [
            'contain' => ['Roles']
        ]);

        if ($this->request->is(['patch', 'post', 'put'])) {

            $user = $this->Users->patchEntity($user,$this->request->getData());
            pr($this->request->getData());
            if ($this->Users->save($user)) {
                $this->Flash->success(__('The user has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
        $roles = $this->Users->Roles->find('list', ['limit' => 200]);
        $this->set(compact('user', 'roles'));
    }

在访问编辑操作时会显示正确的数据。但是在提交数据时(即使没有改变),新记录也会保存到表“角色”中。如何更新控制器操作和相关视图,才能将数据写入“roles_users”表?

【问题讨论】:

  • 这就是您的所有输入/控件吗?如果是这样,则您缺少主键的那些,即id 字段。

标签: cakephp model-view-controller cakephp-3.0 has-and-belongs-to-many


【解决方案1】:

您需要在您的 patchEntity 中添加关联模型,如下所示

$user = $this->Users->patchEntity($user,$this->request->getData(), [
    'associated' => ['RolesUsers']
]);

https://book.cakephp.org/3.0/en/orm/saving-data.html#

【讨论】:

  • 很遗憾,这并不能解决问题
【解决方案2】:
echo $this->Form->control('roles.0._joinData.role_id', ['type'=>'select', 'options' => $roles])

到:

echo $this->Form->control('roles.0.id', ['type'=>'select', 'options' => $roles])
echo $this->Form->control('roles.1.id', ['type'=>'select', 'options' => $roles])

【讨论】:

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