知道何时打电话坚持 [英] knowing when to call persist

查看:75
本文介绍了知道何时打电话坚持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Doctrine 2作为我的ORM,并且一切进展顺利,但是我一直想知道 EntityManager#persist()方法。 持久实体 文档说了以下有关对象 X 调用 persist()的内容:

I'm using Doctrine 2 as my ORM, and things are going well, but I've been wondering about the EntityManager#persist() method. The "Persisting entities" documentation says the following about a call to persist() for object X:


如果X是预先存在的管理实体,则持久操作将忽略它。

If X is a preexisting managed entity, it is ignored by the persist operation.

这使我相信 persist()仅在对象是新对象且尚未保存到数据库时才需要调用。但是,延迟显式更改跟踪策略说:

That leads me to believe that persist() need only be called when the object is new and has not yet been saved to the database. However, the documentation for the "Deferred Explicit" change tracking policy says:


...教义2仅考虑已被通过调用EntityManager#persist(entity)明确标记为进行更改检测...

... Doctrine 2 only considers entities that have been explicitly marked for change detection through a call to EntityManager#persist(entity) ...

...听起来像<$ c必须在对象上调用$ c> persist()才能完全更新该对象。什么时候应该调用 persist()?如果仅在新对象上,无论何时更新实体并让Doctrine整理出差异,调用它是否都会对性能产生重大影响?

... which sounds like persist() must be called on the object for it to be updated at all. When should persist() be called? If only on new objects, is there a significance performance hit to call it anyway whenever an entity is updated and let Doctrine sort out the difference?

推荐答案

使用延迟显式策略(这不是默认策略),您需要在每个已修改实体上显式调用persist(),以使学说得以持久。 (级联持久性关联除外。)

With the Deferred Explicit policy (it's not the default policy), you need to explicitly call persist() on each modified entity for doctrine to persist them. (Except for cascade-persist associations.)

Doctrine仍然需要将每个属性的新值与原始值进行比较,以了解要更新的属性,因此如果您 persist()实体过多,则会对性能产生影响。

Doctrine still needs to compare the new value of each property with the original value to know which property to update, so this may have a performance hit if you persist() too much entities.

使用默认更改跟踪政策需要对尚未由Doctrine管理的实体(您使用 new 创建的实体)进行调用。使用此策略,当您调用flush()原则时,会自动检测到哪些实体已更新并需要保留。

With the default change tracking policy you only need to call persist on entities that are not yet managed by Doctrine (entities that you created with new). With this policy, when you call flush() doctrine automatically detects which entities have been updated and need to be persisted.

这篇关于知道何时打电话坚持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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