Cakephp没有更新模型

时间:2015-01-08 16:21:46

标签: php mysql cakephp

我尝试使用CakePHP框架更新记录,但是我收到一条错误,告诉我无法更新。但我无法找到错误的来源。

这是控制器中if语句失败的地方:

控制器/ 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不能更新我的记录?

1 个答案:

答案 0 :(得分:0)

要进行更新,请求数据必须包含id值

例如

$this->request->data['Invoice']['id'] = $id;

或在您的edit.ctp中添加

$this->Form->hidden('id');