CakePHP 4 Edit 不更新记录
CakePHP4 Edit doesnt update record
版本:4.2.9
我的编辑视图正在用数据填充我的输入,但是当我更改它们并单击“保存”时,它没有保存而是给我“用户已保存”消息。
UsersController.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'));
}
我的edit.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>
您的更新代码不完整,您省略了 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'));
}
版本:4.2.9
我的编辑视图正在用数据填充我的输入,但是当我更改它们并单击“保存”时,它没有保存而是给我“用户已保存”消息。
UsersController.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'));
}
我的edit.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>
您的更新代码不完整,您省略了 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'));
}