Suppose we will be updating a field named 'confirmed' in a model called 'User' with the id of 5. We will then have an array with just that value, like this:
$data = array(
'User' => array(
'id' => 5,
'confirmed' => 1
)
);
We should then pass it on to a save function:
$this->User->save( $data, false, array('confirmed') );
You can RTFM about the save() function here.
9 comments:
You've got some nice tutorials and lessons looking forward for the next post ;P
Nice :)
This code does not update `modified` field
This does not update a field, it creates a new one, why is it that all searches on updating a field in cakephp lead to posts about how to create new fields.
The heading how to update single field in cakephp
not working..
thanks mate , working fine chaeers
Those of you saying it's not working.
Do you remember to set the ID ?
Add to Your AppModel in app directory.
/**
* Set value just for one field in TB
*
* @param int $vNnId - cislo zaznamu
* @param string $sField - name of field
* @param mixed $sValue - value of field
*/
function saveOneField($vNnId, $sField, $sValue) {
$sTable= $this->useTable;
return $this->query("UPDATE $sTable SET $sField = '$sValue' WHERE id = '$vNnId' LIMIT 1");
}
//assuming $this->Post->id is set
$this->Post->saveField('title', 'A New Title for a New Day');
Post a Comment