Cakephp 2.0:控制器中的数组数据(来自post)不可编辑

时间:2012-01-21 15:22:11

标签: php cakephp post crud cakephp-2.0

我有一个简单的crud应用程序, 我的控制器中的编辑页面是这样的:

    function admin_edit($id = null) {

        if (empty($this->data)) {
            $this->data = $this->Product->read(); //product is the model

        } else {
           //its a post request, and $this->data is populated 
            debug($this->data);     

            //i force the id to another id
            $this->data["Product"]["id"] = 115;

            debug($this->data); //the data remains the same, doesnt change.. why?   

           //i will save this later         
        }
}

两个调试都会导致:

Array
(
    [Product] => Array
        (
            [id] => 8
            [alias] => ME
            [order] => 80
            [open_close_images] => 1
            [gallery_id] => 8
            [video_id] => 2
        )
)

后:

Array
(
    [Product] => Array
        (
            [id] => 8 //it must be 115 now!!
            [alias] => ME
            [order] => 80
            [open_close_images] => 1
            [gallery_id] => 8
            [video_id] => 2
        )
)

为什么会这样?

在cakephp 1.3中运行良好,我不明白如何“锁定”该阵列。

1 个答案:

答案 0 :(得分:3)

尝试通过(重新)设置模型的id var设置id,如下所示:

$this->Product->id = 115;

这应该正确更新ID。

修改

如果您正在尝试更新其他值,请使用$this->request->data代替(自CakePHP 2.0以来称之为),例如:

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