How do I pass Token Storage as a param to an Event Listener in Symfony 3.4 How do I pass Token Storage as a param to an Event Listener in Symfony 3.4 symfony symfony

How do I pass Token Storage as a param to an Event Listener in Symfony 3.4


The service definition seems OK, but by default, the EntityListener annotation only support empty constructor.

See in the Doctrine documentation :

An Entity Listener could be any class, by default it should be a class with a no-arg constructor.

A little clarification here :

  • a Doctrine Event Listener is called for all entities (generally require test like $entity instanceof MyClass before doing any action)
  • a Doctrine Entity Listener are used for easy case, and are only called on one entity.

In your code, it seems that you write a common doctrine listener, but use it as an entity listener.

Furthermore, you already declare your service like a common doctrine event with the doctrine.event_listener tag (EntityListener must have the doctrine.orm.entity_listener tag.)

In short, if you just delete the @ORM\EntityListeners annotation, it should be ok.

Note that to get changes when update an entity, you can use the onFlush event. Here is an example with the very useful unitOfWork in order to get an array with all fields that will change in the scheduled updates.

  /**   * @param OnFlushEventArgs $args   */  public function onFlush(OnFlushEventArgs $args)  {      $entityManager = $args->getEntityManager();      $unitOfWork = $entityManager->getUnitOfWork();      // Get all updates scheduled entities in the unit of work.      foreach ($unitOfWork->getScheduledEntityUpdates() as $entity) {          if ($entity instanceof Resource) {              dump( $unitOfWork->getEntityChangeSet($entity) );              // In this array, you'll have only fields and values that will be update.              // TODO : your stuff.          }      }  }


Try putting autowire: true in your services.yml:

services:    # default configuration for services in *this* file    _defaults:        autowire: true      # Automatically injects dependencies in your services.        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.        public: false       # Allows optimizing the container by removing unused services; this also means                            # fetching services directly from the container via $container->get() won't work.                            # The best practice is to be explicit about your dependencies anyway.

Take a look at the docs https://symfony.com/doc/current/service_container/3.3-di-changes.html.