【发布时间】:2012-11-09 09:31:02
【问题描述】:
我有以下问题。我有一个从表单中检索到的对象,并将结果与数据库进行比较,该数据库将旧版本的表单保存为 InspectionReport 对象。
当有旧版本的表单时,将新信息与旧信息合并使用:
// $found_inspection_report is the report found in the database,
// $inspection_report is the one from the form
$found_inspection_report->merge($inspection_report);
然后我想将具有新值的合并对象作为数据库中找到的旧对象保存到数据库中。 因为合并覆盖了 $found_inspection_report 的 ID,所以我首先将旧 ID 设置回来:
$found_inspection_report->setId($old_id);
然后我保存它:
$found_inspection_report->save();
然后我收到以下错误:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'
所以我的问题是,有没有办法成功合并,甚至可能没有设置旧 ID,并更新/保存数据库中的旧对象/记录,比如 ID 1 而不是作为新记录.
【问题讨论】:
标签: php mysql symfony-1.4 doctrine-1.2