简体   繁体   中英

Symfony2 - Doctrine - no changeset in post update

So i am sending an email when a certain value on an entity is changed. I only want the email to send after the update in case the update fails for what ever reason. so on the preUpdate I can do this

public function preUpdate(LifecycleEventArgs $args){

    if ($args->hasChangedField('value') && is_null($args->getOldValue('value'))) {
        $this->sendEmail();
    }

}

but i need to do this on postUpdate and as these methods are not available on postUpdate i refactored it to look like this:

public function postUpdate(LifecycleEventArgs $args){

    $entity      = $args->getEntity();
    $changeSet = $args->getEntityManager()->getUnitOfWork()->getEntityChangeSet($entity);

    if ($entity  instanceof Entity && isset( $changeSet['value'] ) && empty( $changeSet['value'][0] )) {
        $this->sendEmail();
    }
}

However this returns an empty change set, but changes have been made and can be seen in preUpdate. Can anyone see what i am doing wrong? help would be much appreciated :)

On preUpdate event you get event object of class PreUpdateEventArgs where You have change set for entity.

On postUpdate you just get event object of class LifecycleEventArgs where you can ask only for Updated entity (and get latest state of it).

If you want to play with changeset then you need to do it before actual updating entity ( preUpdate event).

A workaround could be to save change set somewhere by yourself and later retrieve it in postUpdate . It is a siplified exaple I've implement once:

<?php

namespace Awesome\AppBundle\EventListener;

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Events;

/**
 * Store last entity change set in memory, so that it could be
 * usable in postUpdate event.
 */
class EntityChangeSetStorageListener implements EventSubscriber
{
    /**
     * @var ArrayCache
     */
    private $cache;

    /**
     * @param ArrayCache $cacheStorage
     */
    public function __construct(ArrayCache $cacheStorage)
    {
        $this->cache = $cacheStorage;
    }

    /**
     * Store last entity change set in memory.
     *
     * @param PreUpdateEventArgs $event
     */
    public function preUpdate(PreUpdateEventArgs $event)
    {
        $entity = $event->getEntity();

        $this->cache->setNamespace(get_class($entity));
        $this->cache->save($entity->getId(), $event->getEntityChangeSet());
    }

    /**
     * Release the memory.
     */
    public function onClear()
    {
        $this->clearCache();
    }

    /**
     * Clear cache.
     */
    private function clearCache()
    {
        $this->cache->flushAll();
    }

    /**
     * {@inheritdoc}
     */
    public function getSubscribedEvents()
    {
        return [
            Events::preUpdate,
            Events::onClear,
        ];
    }
}

Later inject ChangeSetStorage service to the listener where it is necessary on postUpdate event.

I had a really annoying issue with the changeset data, sometimes I got the collection of changes and sometimes not.

I sorted out by adding this line $event->getEntityManager()->refresh($entity); in the prePersist and preUpdate events inside a doctrine.event_subscriber

After the refresh line, changesetdata was updated so the following line started to work:

/** @var array $changeSet */
$changeSet = $this->em->getUnitOfWork()->getEntityChangeSet($entity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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