答案 0 :(得分:17)
如果您只想更新字段,请使用模型的saveField
方法
$this->Order->id = $id;
$this->Order->saveField('status', 'VOID');
参考:http://book.cakephp.org/2.0/en/models/saving-your-data.html
答案 1 :(得分:4)
//Create: id isn't set or is null
$this->Recipe->create();
$this->Recipe->save($this->data);
//Update: id is set to a numerical value
$this->Recipe->id = 2;
$this->Recipe->save($this->data);
请参阅http://book.cakephp.org/2.0/en/models/saving-your-data.html
答案 2 :(得分:0)
要确保记录确实存在,请使用Model::Exists。例如:
function updateFoo($id, $value) {
if (!$this->exists($id) {
return false;
}
$this->id = $id;
return $this->saveField('foo', $value);
}
这样就行了:
$false = $model->updateFoo(false, $value);
$false = $model->updateFoo(0, $value);
$false = $model->updateFoo('doesnotexist', $value);
$true = $model->updateFoo('doesexist', $value);