0

我有以下问题。我有一个从表单中检索到的对象,并将结果与​​数据库进行比较,该数据库将旧版本的表单保存为 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 而不是新记录。

4

1 回答 1

1

使用 synchronizeWithArray 方法而不是合并。请参阅此处的文档

$old->synchronizeWithArray($arrayNew);
$old->save();
于 2012-11-09T12:42:20.013 回答