php - Cakephp不更新模型

标签 php mysql cakephp

我正在尝试使用 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/

相关文章:

php - Laravel 如何创建空关系?

javascript - 提交表单并同时运行函数

javascript - Cakephp 图像未显示在 view.ctp 中

Mysql 在 windows 上慢,在 linux 上快。为什么?

Cakephp从下拉列表中获取值

php - CakePHP关联关系表

php - 选择多个记录并传递它们

php - Cakephp安装

php - 执行 php 脚本时 'asdf' 中的未知列 'where clause'

php - CakePHP getLastInsertId() 返回零