Take the 2-minute tour ×

I am getting error on saving the edited data. Actually User clicked on the edit button User is redirected to edit data page at the end (after) editing the data when user wants to save the edited data cakephp gives error sql integrity violation code 1062. The code for edit is default code generated by the cake bake. the code is

public function edit($id = null) {
    if (!$this->User->exists($id)) {
        throw new NotFoundException(__('Invalid user'));

        if ($this->User->save($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
        } else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
    } else {
        $options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
        $this->request->data = $this->User->find('first', $options);

i also tried savefield instead of save but that is adding new user with all null fields.

share|improve this question
put view file code also!! –  summii Oct 31 '13 at 7:00
can you please post the insertion query created by cake giving you such an error? –  arilia Oct 31 '13 at 7:17
actually view files are empty ctp files because i am using this as a rest service for android phone. –  Naveed Yousaf Oct 31 '13 at 10:55
You probably forgot to set "autoincrement" for the primary key field "id". –  ADmad Nov 3 '13 at 15:10

1 Answer 1

up vote 0 down vote accepted

You should set the id property on the user before you save it:

$this->User->id = $id;

Or make sure that in your $this->request->data;, the id of the object your are editing is present, $this->request->data['User']['id']; in this particular case, the absence of the user id on the request data is causing the problem.

share|improve this answer

Your Answer


