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

查看:151
本文介绍了在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忽略的能力使它不会让人脑筋。

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天全站免登陆