【问题标题】:Symfony 1.4 Merge 2 of the same object and update the old recordSymfony 1.4 合并 2 个相同对象并更新旧记录
【发布时间】: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


    【解决方案1】:

    使用 synchronizeWithArray 方法而不是合并。见这里documentation

    $old->synchronizeWithArray($arrayNew);
    $old->save();
    

    【讨论】:

    • 我对这个解决方案没有进一步了解......问题是我想用新值更新现有对象。但是当我尝试同步或合并数据时,id 会丢失,它只是用新的 id 保存。
    • 我什至无法保存我从数据库中检索到的现有对象而不会收到“SQLSTATE [23000]:完整性约束违规:1062 键 'PRIMARY' 的重复条目 '1-1'”错误
    猜你喜欢
    • 2015-08-23
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2022-12-02
    • 2018-03-04
    相关资源
    最近更新 更多