脚本宝典收集整理的这篇文章主要介绍了php – 更改集Doctrine事件监听器中的嵌入式文档,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
[0]=> object(Tyre24\PRoductBundle\Document\Translations)#1178 (1) { ["translations":protected]=> object(Doctrine\ODM\MongoDB\PErsistentCollection)#1216 (10) { ["snapshot":"Doctrine\ODM\MongoDB\PersistentCollection":private]=> array(0) { } ["coll":"Doctrine\ODM\MongoDB\PersistentCollection":private]=> object(Doctrine\Common\Collections\ArrayCollection)#1217 (1) { ["_elements":"Doctrine\Common\Collections\ArrayCollection":private]=> array(1) { [0]=> object(Tyre24\ProductBundle\Document\Translation)#1227 (3) { ["key":protected]=> string(11) "testkey_new" ["language":protected]=> string(5) "xx_XX" ["value":protected]=> string(9) "testvalue" } } } } } [1]=> object(Tyre24\ProductBundle\Document\Translations)#1178 (1) { ["translations":protected]=> object(Doctrine\ODM\MongoDB\PersistentCollection)#1216 (10) { ["snapshot":"Doctrine\ODM\MongoDB\PersistentCollection":private]=> array(0) { } ["coll":"Doctrine\ODM\MongoDB\PersistentCollection":private]=> object(Doctrine\Common\Collections\ArrayCollection)#1217 (1) { ["_elements":"Doctrine\Common\Collections\ArrayCollection":private]=> array(1) { [0]=> object(Tyre24\ProductBundle\Document\Translation)#1227 (3) { ["key":protected]=> string(11) "testkey_new" ["language":protected]=> string(5) "xx_XX" ["value":protected]=> string(9) "testvalue" } } } } } }
这里,更改集数组的第一个元素应该反映嵌入文档的旧状态,但它总是在两个数组索引中显示相同的(新)文档.对于没有嵌入文档的文档,它可以正常工作.任何的想法?
我赞成将OnFlush事件挂钩,因为它是一个可以在实际发生之前找到所有写操作的单一时间点.因此,您可以获得即将发生的一系列一致的更改.
使用ORM,使用PostPersist,PostUpdate和PostDelete,您将会迟到,因为更改已经发生,并且无法获得可靠的更改集.
使用PrePersist,PreUpdate和Predelete,您将无法获得单个时间点:PreUpdate和PreDelete将在刷新操作中发生,但只要您在实体/文档上调用persist()就会发生PrePersist.
因此,在OnFlush监听器中,我将收集所有实体/文档更改以及关联更改.我将这些保留在内存中,直到调度PostFlush事件. PostFlush事件发生在一切都持久存储到数据库之后,因此这是写日志的安全点.换句话说:当刷新操作因任何原因失败时,将不会写入日志,因为我们实际上没有持久化任何内容.
跟踪什么
使用ORM,始终会检测到标量属性的更改.但是通过引用检测对象的更改.这意味着如果属性包含新对象,则会检测到更改.但是当属性包含相同的对象时(尽管该对象本身已更改),将无法检测到更改.
这就是为什么,至少使用ORM,具有更改集合的实体将不会显示在$uow-> getScheduledEntityInsertions()等的返回值中.我不确定,但我怀疑ODM在同样的方式.
因此,当跟踪侦听器中的更改时,您将必须使用所有这些方法:
> $uow-> getScheduledDocumentInsertions();
> $uow-> getScheduledDocumentUpserts();
> $uow-> getScheduledDocumentUpdates();
> $uow-> getScheduledDocumentDeletions();
> $uow-> getScheduledCollectionDeletions();
> $uow-> getScheduledCollectionUpdates();
其中最后两个将返回已更改的集合数组.从集合中,您可以获得拥有实体/文档($col-> getOwner()),并使用diff方法($col-> getInsertDiff()和$col-> getDeleteDiff())来找出完全改变了.
我希望这可以帮助您找到解决方案!
以上是脚本宝典为你收集整理的php – 更改集Doctrine事件监听器中的嵌入式文档全部内容,希望文章能够帮你解决php – 更改集Doctrine事件监听器中的嵌入式文档所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。