0

i have this subscriber located in AppBundle\DoctrineListeners body of it is this

namespace AppBundle\DoctrineListeners;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\OnFlushEventArgs;
use AppBundle\Entity\Bing\KeyWordReport;
use Doctrine\ORM\Events;

/**
 * Listener used to on update fielt create history
 */
class KeywordSubscriber implements EventSubscriber
{
    public function onFlush(OnFlushEventArgs $args)
    {
        throw new \Exception("Error Processing Request", 1); //for testing 

        $em  = $args->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityUpdates() as $updated) {
            if ($updated instanceof KeyWordReport) {
                //do some work
            }
        }

        $uow->computeChangeSets();
    }

    public function getSubscribedEvents()
    {
        return [Events::onFlush => ['onFlush', 10], Events::preUpdate];
    }
}

i'm using autoconfigure introduced in symfony 3.3

service.yml

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

in Entity i have in annotation this * @ORM\EntityListeners({"AppBundle\DoctrineListeners\KeywordSubscriber"}) why it is not executed?

Viszman
  • 1,378
  • 1
  • 17
  • 47
  • Doctrine `EventSubscribes` (http://symfony.com/doc/current/doctrine/event_listeners_subscribers.html) and `EntityListeners` (https://symfony.com/doc/current/bundles/DoctrineBundle/entity-listeners.html) are different things. And here, there is nothing to do with autowiring (your subscriber has no dependency). But if you are in 3.3, you can use service autoloading and autoconfiguring to automatically register this class as a service with appropriate tag. – Yann Eugoné Jul 26 '17 at 07:21
  • Possible duplicate of [Symfony 4 Doctrine EventSubscriber not used](https://stackoverflow.com/questions/51234927/symfony-4-doctrine-eventsubscriber-not-used) – Fabien Salles Aug 01 '19 at 08:52

1 Answers1

0

As @YannEugoné noted in comment – EventSubscribes and Doctrine EntityListeners are different things. EventListeners in Symfony have be registered manually:

namespace AppBundle\DoctrineListeners;

use Doctrine\ORM\Event\OnFlushEventArgs;
use AppBundle\Entity\Bing\KeyWordReport;

class KeywordListener
{
    public function onFlush(OnFlushEventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();

        foreach ($uow->getScheduledEntityInsertions() as $entity) {
            // … some action
        }

        foreach ($uow->getScheduledEntityUpdates() as $entity) {
            // … some action
        }

        foreach ($uow->getScheduledEntityDeletions() as $entity) {
            // … some action
        }

        foreach ($uow->getScheduledCollectionDeletions() as $col) {
            // … some action
        }

        foreach ($uow->getScheduledCollectionUpdates() as $col) {
            // … some action
        }
    }
}
#services.yml
services:
    # …

    AppBundle\DoctrineListeners\KeywordListener:
        tags:
            - { name: doctrine.event_listener, event: onFlush }
jkucharovic
  • 4,214
  • 1
  • 31
  • 46
  • i have iserted your service code but still i don't see that `onFlush` function is called – Viszman Jul 26 '17 at 07:40
  • @Viszman Oh, I overlooked that. You have mixed Symfony event subscriber, Doctrine event subscriber and Doctrine event listener. I've updated code. – jkucharovic Jul 26 '17 at 07:52
  • When i change name in service.yml then i get error that loading failed, so class is loaded. i get events like preUpdate and postUpdate executed but i want to chave access to both properties, or minimal to old, as i readed i can do is then event `onFlush` is dispatched by my class function is not called – Viszman Jul 26 '17 at 08:10
  • listener.appBundel: class: AppBundle\Listener\KeywordListener tags: - { name: doctrine.event_listener, event: onFlush } above helped – Viszman Jul 26 '17 at 08:45
  • @Viszman if you need exactly `onFlush` event, you need listener (because `onFlush` is not Lifecycle event) as is [stated in documentation](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#onflush). – jkucharovic Jul 26 '17 at 08:50