implementing quot;update if existsquot; in Doctrine ORM(实现“如果存在则更新在教义 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 包会出现这种问题吗?或者是否有任何 Doctrine 专家知道如何通过黑客或任何方式实现这一目标?
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?
推荐答案
根据 https://www.vivait.co.uk/labs/updating-entities-when-an-insert-has-a-duplicate-key-in-原则这可以通过 $entityManager->merge()
实现.
According to https://www.vivait.co.uk/labs/updating-entities-when-an-insert-has-a-duplicate-key-in-doctrine this can be achieved with $entityManager->merge()
.
$entity = new Table();
$entity->setId(1);
$entity->setValue('TEST');
$entityManager->merge($entity);
$entityManager->flush();
这篇关于实现“如果存在则更新"在教义 ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实现“如果存在则更新"在教义 ORM
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01