Doctrine merge: DateTime field always updated(教义合并:日期时间字段始终更新)
问题描述
我用现有的 Id
创建了一个新实体,我想更新相关的数据库记录.
I create a new Entity with an existing Id
, and I want to update the related database record.
学说合并一直是我最好的朋友:识别是否有更改并生成正确的更新查询.
Doctrine merge has been my best friend: recognizes if there are changes and generates the correct update query.
$entity = new Entity();
$entity->setId(1);
$entity->setName('test');
$EntityManager->merge($entity);
$EntityManager->flush();
假设 id=1 的元素已经存在于 db 中:如果名称与 'test' 不同,Doctrine 会生成这个查询:
Suppose that the element with the id=1 already exists in the db: if the name is different from 'test', Doctrine generates this query:
UPDATE table SET name = ? WHERE id = ? ["test","1"]
如果我再次运行上面的代码,Doctrine 会发现没有任何变化,也没有提交任何查询.
If I run again the code above, Doctrine recognizes that nothing is changed, and no query is committed.
但是...当我设置一个 DateTime 字段时,Doctrine 认为它已更改并始终运行更新查询:
But... when I set a DateTime field, Doctrine consider it as changed and always runs the update query:
$entity = new Entity();
$entity->setId(1);
$entity->setDate(new DateTime(2000-01-01));
$EntityManager->merge($entity);
$EntityManager->flush();
//* ALWAYS commits this query:
>> UPDATE table SET date = ? WHERE id = ? ["2000-01-01 00:00:00","1"]
您知道避免这种无用更新的方法吗?谢谢!
Do you know a way to avoid this useless update? Thanks!
推荐答案
正如@Raymond 所说,这是预期的行为.
As said by @Raymond, it's the expected behavior.
很遗憾,但如果您的合并不依赖于 date
属性,解决方法可能是在合并后设置日期,如下所示:
It's a pity, but if your merge doesn't depend on the date
property, a workaround could be to set the date after the merge like this:
$entity = new Entity();
$entity->setId(1);
// Set all fields that the merge is depending on
// ...
$EntityManager->merge($entity);
$entity->setDate(new DateTime('2000-01-01'));
$EntityManager->flush();
更新
尝试后,唯一的选择似乎是检索合并的对象并通过再次刷新 EntityManager 来更新它.
After tried, the only alternative seems to retrieve the merged object and update it by flushing the EntityManager again.
你可以使用:
$entity = new Entity();
$entity->setId(1);
// Set all fields that the merge is depending on
$EntityManager->merge($entity);
$EntityManager->flush();
$entity = $EntityManager->getRepository('YourEntity')->find(1); // Retrieve the entity
$entity->setDate(new DateTime('2000-01-01'));
$EntityManager->flush(); // Reflush
更新2
我发现在合并后实现更新的更简洁的方法是重新合并实体,例如:
The cleaner way I found to achieve the update after the merge is re-merge the entity, e.g. :
$entity = new Entity();
$entity->setId(1);
// Set all fields that the merge is depending on
$EntityManager->merge($entity); // The real merge that retrieve (without commit) or create
$EntityManager->flush();
$entity->setDate(new DateTime('2000-01-01'));
$entityManager->merge($entity); // Remerge the object with the changed field
$EntityManager->flush(); // Working re-flush
但这并没有改变主要问题,也没有真正的意义,因为你不能自己比较 DateTime
对象,$entity->getDate()
在调用 setDate
之前总是返回 null,即使在第一次合并之后也是如此.
But this doesn't change the main problem and doesn't really make sense because you cannot compare the DateTime
object by yourself, $entity->getDate()
return always null before calling setDate
, even after the first merge.
Doctrine 通过引用(哈希)与 ===
比较对象,即使对象日期未更改,DateTime
的新实例也会导致更新.
Doctrine compare the objects by reference (hash) with ===
, also a new instance of DateTime
causes the update even if the object date is unchanged.
这是一个真正有问题的问题,可以通过使用 ==
作为比较运算符来解决,但是教义不能在不破坏它们的情况下为 DateTime
制定特定条件通用对象比较机制,涉及降低最常用功能之一的性能.
This is a really problematic issue that could be fixed by using ==
as comparison operator, but doctrine can't make a specific condition for DateTime
without breaking their generic object comparison mechanism, that involves to decrease performances of one of the most used feature.
这篇关于教义合并:日期时间字段始终更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:教义合并:日期时间字段始终更新
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01