Reputation: 38432
How do i force cake to update a field and not insert a new record.
It should fail if the id does not exist in the db
I found to force a insertion i can do 'updated' => false
so if i do 'updated' => true
will it work
Upvotes: 7
Views: 26014
Reputation: 66358
To ensure that the record does exist, use Model::Exists. For example:
function updateFoo($id, $value) {
if (!$this->exists($id) {
return false;
}
$this->id = $id;
return $this->saveField('foo', $value);
}
This would act like so:
$false = $model->updateFoo(false, $value);
$false = $model->updateFoo(0, $value);
$false = $model->updateFoo('doesnotexist', $value);
$true = $model->updateFoo('doesexist', $value);
Upvotes: 0
Reputation: 50029
If you want to just update a field, use the saveField
method of the model
$this->Order->id = $id;
$this->Order->saveField('status', 'VOID');
Reference : http://book.cakephp.org/2.0/en/models/saving-your-data.html
Upvotes: 17
Reputation: 18139
//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);
see http://book.cakephp.org/2.0/en/models/saving-your-data.html
Upvotes: 4