Reputation: 7495
I'm trying to find an item and update it if it exists, or to create a new one if it does not exist. However, for some reason it seems to be trying to create a new object instead of updating in the event that that already exists in the database.
$object = ObjectItem::firstOrNew(array('object_item_id'=>$userEditedObject['object_item_id'], 'object_id'=>$object_id));
$object->setFields($userEditedObject);
if($object->save()){
return TRUE;
} else {
return FALSE;
}
That code appears to be producing the error
"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '113' for key 'PRIMARY'
This is strange, because I have used this before and it worked okay -- it just seems to be in this particular case.
Upvotes: 5
Views: 2010
Reputation: 8663
It means that array $userEditedObject contains duplicate value for the primary key, which is usually "id".
You get $object and try to edit its primary key so that other row exists with same primary key causing the failure.
See what field is primary key for you application and make sure you are not creating any duplicates.
Upvotes: 2