0

In Cakephp3, I have the following tables:

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;

My intention is, to edit the available fields in table 'roles_users' within the Users add & edit view (and the related controller actions). I considereded https://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs, my user view (edit) look like:

<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>

the underlying controller action is:

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'));
    }

The correct data are displayed, while accessing the edit action. But when submitting the data (even unchanged) new records are saved to table 'roles'. How do I have to update the controller actions and the related views, to write data to the table 'roles_users'?

sophros
  • 14,672
  • 11
  • 46
  • 75
florian
  • 3
  • 2
  • Are that all your inputs/controls? If so, you are missing those for the primary keys, ie the `id` fields. – ndm Aug 10 '18 at 12:34

2 Answers2

0

You need to add associated model in your patchEntity like below

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

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

Sehdev
  • 5,486
  • 3
  • 11
  • 34
0
echo $this->Form->control('roles.0._joinData.role_id', ['type'=>'select', 'options' => $roles])

to:

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