Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update an existing record in CakePHP?

Tags:

cakephp

I am using CakePHP and following its tutorial. I want to update a record but when i do its create another record not updating. according to tutorial my code is given below

$data = array('Id' => $id, 'Approved' => 12);
$this->names->save($data);

it results in

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 4 for key PRIMARY

And if I do this

$this->names->Id=$id;

It adds a new record. How should I fix this ?

like image 704
user3337590 Avatar asked Mar 17 '14 11:03

user3337590


2 Answers

$this->names->id=$id;                
$this->names->set(array('Approved'=>12));                
$this->names->save();
like image 156
Muhammad Ali Hassan Avatar answered Nov 02 '22 20:11

Muhammad Ali Hassan


The key must be id and not Id. If in your table you can't use id (lowercase) and you have to use Id (uppercase) then you have to set it in your Model file

also you are not followeing the conventions: the model should be Name and not names (singular and CamelCase)

like image 33
arilia Avatar answered Nov 02 '22 22:11

arilia