Doctrine: ON DUPLICATE KEY UPDATE

后端 未结 9 1141
有刺的猬
有刺的猬 2020-12-18 19:43

How can I write an INSERT doctrine query with option ON DUPLICATE KEY UPDATE?

9条回答
  •  时光取名叫无心
    2020-12-18 20:19

    I had the same problem and after investigating a bit it looks like Doctrine doesn't do it. My solution was to do a findBy before my insert to see if any records exist with the unique fields. If this returns an entity then I update that entity and persist it instead of creating a new entity to persist.

    If you are concerned about performance then this is not ideal as we are doing a select before every insert. However since Doctrine is database agnostic it is the only alternative to locking yourself to MySQL. It's one of those tradeoffs: do you want performance or portability.

提交回复
热议问题