我正在创建一个可以让多个用户同时编辑相同信息的 CakePHP 应用程序。为了防止用户覆盖彼此的更改,我想让它在用户保存表单时只保存用户更改的一两个字段。
我不能只将这些字段与数据库值进行比较,因为这些数据库值实际上可能是来自其他用户的较新值。
CakePHP 是否提供任何方式仅通过 POST 发送更新的字段?
我不相信有一个内置的解决方案。我想出了一些有效的代码,但您肯定希望将其放入自己的函数中,以便多个控制器操作可以使用它。此外,它并不完美。例如,它会在日期字段上失败,因为 CakePHP 的日期字段select
在表单中呈现为 s。
这是用于编辑操作。原始代码:
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
}
}
修改后的代码:
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$originalData = unserialize(base64_decode($this->request->data['Extra']['original_data']));
$save = $this->request->data;
unset($save['Extra']);
foreach ($save as $model => $modelFields) {
if (!array_key_exists($model, $originalData)) {
continue;
}
foreach ($modelFields as $field => $value) {
if (!array_key_exists($field, $originalData[$model])) {
continue;
}
if ($save[$model][$field] === $originalData[$model][$field]) {
unset($save[$model][$field]);
}
}
}
$this->User->set($save);
if ($this->User->validates() && $this->User->save($save, false)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
$this->request->data['Extra']['original_data'] = base64_encode(serialize($this->request->data));
}
}
另外,在表格中的某处添加edit.ctp
:
echo $this->Form->hidden('Extra.original_data');