实施“更新(如果存在)”在原则ORM [英] implementing "update if exists" in Doctrine ORM

查看:110
本文介绍了实施“更新(如果存在)”在原则ORM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在一个事务中 INSERT OR UPDATE IF EXISTS

I am trying to INSERT OR UPDATE IF EXISTS in one transaction.

mysql 中,我通常会使用 DUPLICATE KEY (UPDATE ON DUPLICATE KEY )我知道使用各种SQL变体和子查询的这个问题的许多解决方案,但我试图在Doctrine(PHP ORM)中实现这一点。看起来会有这样做的Doctrine方法,因为它是如此的功能包装,但我没有找到任何东西。某些原因使用PHP ORM包这种事情是一个问题吗?或者任何教义专家知道如何通过黑客或任何方式实现这一点?

in mysql, I would generally use DUPLICATE KEY ("UPDATE ON DUPLICATE KEY".) I'm aware of many solutions to this problem using various SQL variants and sub-queries, but I'm trying to implement this in Doctrine (PHP ORM). It seems there would be Doctrine methods for doing this since it's so feature packed, but I'm not finding anything. Is this sort of thing a problem using PHP ORM packages for some reason? Or do any Doctrine experts know how to achieve this through hacks or any means?

推荐答案

唯一可以想到的是先查询实体是否存在,否则创建新的实体。 >

The only thing I can think of is to query first for the entity if it exists otherwise create new entity.

if(!$entity = Doctrine::getTable('Foo')->find(/*[insert id]*/))
{
   $entity = new Foo();
}
/*do logic here*/
$entity->save();

这篇关于实施“更新(如果存在)”在原则ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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