0

I have the following problem. I'm having an object which I retrieved from the form, and I compare the results to the database which has an older version of form saved as an InspectionReport object.

When there is an older version of the form, merge the new information with the old using:

// $found_inspection_report is the report found in the database, 
// $inspection_report is the one from the form
$found_inspection_report->merge($inspection_report);

Then I'd like to save the merged object with the new values to the database as the old found object from the database. Because the merge overwrited the ID of the $found_inspection_report I first set the old ID back:

$found_inspection_report->setId($old_id);

And then I'm saving it:

$found_inspection_report->save();

But then I get the following error:

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

So my question is, is there a way to succesfully merge, maybe even without the old ID to be set back, and update/save the old object/record in the database, say with ID 1 and not as a new record.

hakre
  • 193,403
  • 52
  • 435
  • 836
CE_REAL
  • 384
  • 5
  • 13

1 Answers1

1

Use the synchronizeWithArray method instead of merge. See here the documentation

$old->synchronizeWithArray($arrayNew);
$old->save();
turbod
  • 1,988
  • 2
  • 17
  • 31
  • I'm not getting much further with this solution... The problem is that I want to update the exisiting object with the new values. But when I try to synchronize or merge the data, the id gets lost and it just saves with a new id. – CE_REAL Nov 15 '12 at 13:45
  • I can't even save my existing object I retrieved from the database without getting an "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-1' for key 'PRIMARY'" error – CE_REAL Nov 15 '12 at 14:52