Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cakephp Update field

Tags:

php

cakephp

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

like image 559
aWebDeveloper Avatar asked Mar 05 '11 07:03

aWebDeveloper


2 Answers

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

like image 192
JohnP Avatar answered Sep 22 '22 21:09

JohnP


//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

like image 29
Brandon Frohbieter Avatar answered Sep 20 '22 21:09

Brandon Frohbieter