对 Doctrine2/Symfony2 中的重复条目插入忽略 [英] insert ignore on duplicate entries in Doctrine2/Symfony2

查看:25
本文介绍了对 Doctrine2/Symfony2 中的重复条目插入忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Doctrine2 忽略重复条目?

How to ignore on duplicate entries using Doctrine2?

错误示例:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'symfony' for key 'UNIQ_389B783389B783'

推荐答案

这是 Doctrine 的麻烦之一,无法执行 INSERT/UPDATE 忽略,有一些解决方法,例如创建检查该行是否存在的方法,以及如果是,则跳过它.

That's one of the nuisances of Doctrine, it's not possible to do INSERT/UPDATE Ignore, there are workaround like creating a methods that checks if the row exists, and if it does then just skip it.

您可以捕获异常,这样您的脚本就不会以异常结束.但是,实体管理器将关闭,您将无法再使用它.不过,您仍然可以使用 PDO,并且您可以在数据库中插入一条记录,表明您的批处理失败,因为 X 需要重新启动(这是我通常做的).

You can catch the exception, so that your script doesn't end in an exception. However, the entity manager will be closed and you will not be able to use it anymore. You can still use PDO, though and you can insert a record in the database indicating that your batch failed because X and it needs to be restarted (that's what I usually do).

如果以上选项都不适合您,最终我会编写原始 SQL 来进行批处理并且我根本不使用 Doctrine,它最终会更快并且执行 INSERT/UPDATE Ignore 的能力使其成为可能毫无疑问.

If none of the options above work for you, ultimately I end up writing raw SQL to do batch processing and I don't use Doctrine at all, it ends up being faster and the ability of doing INSERT/UPDATE Ignore makes it a no brainer.

这篇关于对 Doctrine2/Symfony2 中的重复条目插入忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆