简体   繁体   English

typo3 flow isDirty在模型上

[英]typo3 flow isDirty on model

Im trying to find out which attributes of an entity have been changed. 我试图找出实体的哪些属性已更改。 As far I have seen, there is a PersistenceSession with a method to check an object if an attribute isDirty. 据我所知,有一个PersistenceSession带有一种方法来检查对象是否为isDirty属性。 But its always true because it never registers the old object. 但是它总是正确的,因为它从不注册旧对象。

So if I take the demo from the QuickGuide and override the update method in the CoffeeBeanRepository: 因此,如果我从QuickGuide中获取演示并覆盖CoffeeBeanRepository中的update方法:

/**
 * @param \Acme\Demo\Domain\Model\CoffeeBean $coffeeBean
 */
public function update($coffeeBean) {
    \TYPO3\Flow\var_dump($this->persistenceSession->isDirty($coffeeBean, 'name'), "name changed before");
    parent::update($coffeeBean);
    \TYPO3\Flow\var_dump($this->persistenceSession->isDirty($coffeeBean, 'name'), "name changed after");
}

... its always TRUE (both), despite I didn't change anything. ...尽管我没有做任何更改,但它始终都是TRUE。

Anyone an idea/reference how this can be accomplished? 任何人都有想法/参考如何实现? I am using it for a REST API where a user can't update several fields and on editing of some fields additional actions have to be executed. 我将其用于REST API,其中用户无法更新多个字段,并且在编辑某些字段时必须执行其他操作。

The persistenceSession is part of the generic persistence backend of Flow and is neither maintained, nor really used unless you explicitly deactivate doctrine. persistenceSession是Flow通用持久化后端的一部分,除非您显式停用该理论,否则既不会维护它,也不会真正使用它。 Hence persistenceSession will not help you, because all entities are considered new for the persistenceSession as you noticed. 因此,persistenceSession将无济于事,因为您注意到,所有实体对于persistenceSession都是新的。

With doctrine you need to get the entity changeset from the "UnitOfWork", which you can get from an injected \\Doctrine\\Common\\Persistence\\ObjectManager. 使用该学说时,您需要从“ UnitOfWork”中获取实体变更集,可以从注入的\\ Doctrine \\ Common \\ Persistence \\ ObjectManager中获得该变更集。 See also Is there a built-in way to get all of the changed/updated fields in a Doctrine 2 entity 另请参阅是否有内置方法来获取Doctrine 2实体中的所有已更改/更新的字段

However, this is a suboptimal solution and a hacky work-around at best. 但是,这是次优的解决方案,并且充其量是一个棘手的解决方法。 If you need to track changes to your entity, it should be an explicit part of your domain model. 如果您需要跟踪对实体的更改,则它应该是域模型的明确组成部分。 For example make your setters record a changed properties list, when the given value is different from the current. 例如,当给定值与当前值不同时,让设置员记录更改后的属性列表。 When done, you could even optimize doctrines change tracking on the way with that: http://doctrine-orm.readthedocs.org/en/latest/reference/change-tracking-policies.html#notify 完成后,您甚至可以通过以下方式优化教义更改跟踪: http : //doctrine-orm.readthedocs.org/en/latest/reference/change-tracking-policies.html#notify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM