我有一个简单的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中运行良好,我不明白如何“锁定”该阵列。
答案 0 :(得分:3)
尝试通过(重新)设置模型的id var设置id,如下所示:
$this->Product->id = 115;
这应该正确更新ID。
修改强>
如果您正在尝试更新其他值,请使用$this->request->data
代替(自CakePHP 2.0以来称之为),例如:
$this->request->data['Product']['id'] = 115;