0
votes

I'm using CakePHP v2.3.5. When I update the table

`users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(320) NOT NULL,
  `password` varchar(45) NOT NULL,
  `firstname` varchar(255) DEFAULT NULL,
  `lastname` varchar(255) DEFAULT NULL,
  `country` varchar(255) DEFAULT 'SG',
   PRIMARY KEY (`id`)
);

using $this->User->save($this->request->data), it always fires an insert command and throws

"Integrity constraint violation: 1062 Duplicate entry '10' for key 'PRIMARY'"

The request->data object is:

array(
    'User' => array(
        'id' => '10',
        'firstname' => 'Someone',
        'lastname' => 'Surname',
    )
)

The complete action I'm using now is:

public function addInfo() {
    if ($this->request->is('post')) {

        $this->User->create();
        $this->User->id=(int)$this->request->data['User']['id'];
        if ($this->User->save($this->request->data)) {

        }

    }
}

And the beforeSave function in Model is:

public function beforeSave($options = array()) {

   if (isset($this->data['User']['password'])) {
       $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
   }
   return true;

}

In User Object, this id=>false looked fishy to me

object(User) { displayField => 'firstname' primaryKey => 'id' useDbConfig => 'default' useTable => 'users' id => false data => array( [maximum depth reached] ) }

5
Try with 'id'=>10 (10 as integer) in the request->data arrayNunser
Thanks for editing the post. Casting id as integer, 'User' => array('id' => (int) 10,'firstname' => 'Someone','lastname' => 'Surname') but problem persist.user2078719
Please show your entire controller action code. Just quoting the save method you are using isn't going to help us help you.Oldskool
what happens if you hardcode $this->User->id = 10; right before the save action?Alex Stallen
I hardcoded $this->User->id, still the same. Then I try hardcoded the user id followed by unset($this->request->data['User']['id']), it inserted as a new row.user2078719

5 Answers

2
votes

Do a create before the save

$this->User->create();
$this->User->save($this->request->data); //with the id cast as integer

create is for reseting the model, so even if it is an update, you should call that function as a precaution.

0
votes

Try setting the ID before saving:

$this->User->id = useridhere
$this->Users->save($this->request->data);
0
votes

If you want to update a value, rather than create a new one, make sure your are passing the primary key field into the data array:

$data = array('id' => 10, 'firstname' => 'Someone', 'lastname'=> 'Surname');
// This will update User with id 10

$this->User->save($data);
0
votes

Found the reason. I happened to have a function in Model called getId and CakePHP was also using getId to check the existence of the row, hence, it always got negative result when checking by id so it fired an insert call instead of update call. Thanks everyone for helping.

0
votes

I encountered same problem here. I've tried different configuration (with save(), saveAll(), saveMany(), saveAssociated(), with deep option), later I found out that my table has different primary key (the primary field name wasn't "id"). Adding primary field name to my model solves the problem.

class SomeModel extends AppModel {

    public $primaryKey = "different_primary_field_name_for_id";

}