Caute, Neviete mi poradit mam listener na onFlush a v nom sa snazim editovat dalsie entity. podla tochto navodu som si chcel odregistrovat lisener urobit zmenu ktoru potrebujem a znovu ho zaregistrovat. Problem je v tom ze mi to nadava: Undefined index: Doctrine\ORM\Event::onFlush tu

Ja mam asi takyto listener, neviete mi poradit co robim zle?

<?php
namespace Nas\CmsModule\Listeners;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Query;
use Kdyby\Events\Subscriber;
use Nas\CmsModule\Model\BasePost;
use Nas\CmsModule\Model\Route;
use Nas\CmsModule\Model\RouteRepository;

class RouterListener implements Subscriber {

    /**
     * Specifies the list of events to listen
     * @return array
     */
    public function getSubscribedEvents() {
        return array(
            Events::onFlush
        );
    }

    /**
     * @param OnFlushEventArgs $args
     */
    public function onFlush(OnFlushEventArgs $args) {
        /** @var \Kdyby\Doctrine\EntityManager $em */
        $em = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        // Remove listener
        $eventManager = $em->getEventManager();
        if ($eventManager) {
            $eventManager->removeEventListener(array(Events::onFlush), $this);
        }

        // Process
        foreach ($uow->getScheduledEntityUpdates() as $key => $entity) {
            if ($entity instanceof Route) {
                $changeSet = $uow->getEntityChangeSet($entity);

                if (array_key_exists('url', $changeSet) || array_key_exists('language', $changeSet)) {
                    $this->replaceChildWhenUpdate($entity, $em);
                }
            }
        }

        // Add listener back again
        if ($eventManager) {
            $eventManager->addEventListener(array(Events::onFlush), $this);
        }
    }

}

You shouldn't change entities in postflush. It's too late and requires a second flush call to actually work. Some people are calling flush in the subsriber but that can end very badly in some cases because the first flush is not yet finished.

Personally I'm using “double flush” with my own event:

$this->em->transactional(
    function () {
        // some logic
        $this->em->flush();
        // use symfony/event-dispatcher to call subsribers with more logic
        $this->eventDispatcher->dispatch(...);
        // transactional will call a second flush
    }
)

You must first log in to participate in this discussion