flow-framework

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. 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:

/**
 * @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.

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.


Solution

  • The persistenceSession is part of the generic persistence backend of Flow and is neither maintained, nor really used unless you explicitly deactivate doctrine. Hence persistenceSession will not help you, because all entities are considered new for the persistenceSession as you noticed.

    With doctrine you need to get the entity changeset from the "UnitOfWork", which you can get from an injected \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

    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