我正在尝试使用 CakePHP 框架更新记录,但收到一条错误消息,告诉我无法更新。但我找不到错误来自哪里。
这是 Controller 中 if 语句失败的地方:
Controller /InvoicesController.php
if ($this->request->is(array('post', 'put'))) {
$this->Invoice->id = $id;
if ($this->Invoice->save($this->request->data)) {
$this->Session->setFlash(__('Invoice updated'), 'flash-success');
} else {
$this->Session->setFlash(__('Please try again'), 'flash-error');
debug($this->validationErrors, true);
pr($this->request->data);
}
}
调试结果如下:
/app/Controller/InvoicesController.php (line 73)
null
这是我尝试保存时来自 CakePHP 的数据:
Array
(
[Invoice] => Array
(
[date] => Array
(
[day] => 05
[month] => 01
[year] => 2015
)
[period] => 14
[notes] =>
)
)
为什么 CakePHP 无法更新我的记录?
最佳答案
对于更新,请求数据必须包含id值
示例
$this->request->data['Invoice']['id'] = $id;
或者在你的edit.ctp中添加
$this->Form->hidden('id');
关于php - Cakephp不更新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27844776/