CakePHP4编辑不更新记录

m0rkklqb  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(147)

版本号:4.2.9
我的编辑视图正在用数据填充我的输入,但是当我更改它们并单击“保存”时,它没有保存,而是给我一条“用户已保存”消息。
用户控制器. php编辑函数

public function edit($id = null)
    {
        $user = $this->Users->get($id, [
            'contain' => ['Userdata'],
        ]);
        if ($this->request->is(['post', 'put'])) {
          $user = $this->Users->get($id, [
              'contain' => ['Userdata'],
          ]);
            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.'));
        }
        $this->set(compact('user'));
    }

我的编辑. php

<div class="users form large-9 medium-8 columns content">
    <?php echo $this->Form->create($user) ?>
    <fieldset>
        <legend><?= __('Edit User') ?></legend>
        <?php
            echo $this->Form->control('userdata.Email');
            echo $this->Form->control('userdata.UserName');
            echo $this->Form->control('PasswordHashed', ['type' => 'password']);
        ?>
    </fieldset>
    <?= $this->Form->button(__('save')) ?>
    <?= $this->Form->end() ?>
</div>
bxpogfeg

bxpogfeg1#

您的更新程式码不完整,您已省略patchEntity方法。

public function edit($id = null)
{
    // call query only once
    $user = $this->Users->get($id, [
        'contain' => ['Userdata'],
    ]);
    // Call the debug method just to test and understand your data
    // debug($user);
    // debug($this->getRequest()->getData()); // debug posted data
    if ($this->request->is(['post', 'put'])) {
      $user = $this->Users->patchEntity($user, $this->getRequest()->getData());
        // debug patched data debug($user); exit;
        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.'));
    }
    $this->set(compact('user'));
}

相关问题