Mirage
Mirage

Reputation: 31548

How can I add multiple events in services.yml file as event Listeners in Doctrine symfony

I am using this:

my.listener:
        class: Acme\SearchBundle\Listener\SearchIndexer
        tags:
            - { name: doctrine.event_listener, event: postPersist }

Now if I try to listen for two events like this:

- { name: doctrine.event_listener, event: postPersist, preUpdate }

it gives an error.

Upvotes: 11

Views: 5373

Answers (3)

Oussama Boukhchim
Oussama Boukhchim

Reputation: 1

Lifecycle subscribers are deprecated starting from Symfony 6.3. So you can use this :

Acme\SearchBundle\Listener\SearchIndexer:
        tags:
            -
                {
                  name: 'doctrine.event_listener',
                
                  # this is the only required option for the lifecycle listener tag
                  event: 'postPersist',
                  
                  # listeners can define their priority in case multiple subscribers or listeners are associated
                  # to the same event (default priority = 0; higher numbers = listener is run earlier)
                  priority: 500,

                  # you can also restrict listeners to a specific Doctrine connection
                  connection: 'default'
                }
                
            -   {
                  name: 'doctrine.event_listener',
                
                  # this is the only required option for the lifecycle listener tag
                  event: 'postUpdate',
                  
                  # listeners can define their priority in case multiple subscribers or listeners are associated
                  # to the same event (default priority = 0; higher numbers = listener is run earlier)
                  priority: 500,

                  # you can also restrict listeners to a specific Doctrine connection
                  connection: 'default'
                }

Upvotes: 0

mask8
mask8

Reputation: 3638

I think you can do like this:

my.listener:
        class: Acme\SearchBundle\Listener\SearchIndexer
        tags:
            - { name: doctrine.event_listener, event: postPersist }
            - { name: doctrine.event_listener, event: preUpdate }

Upvotes: 24

Adrian Schneider
Adrian Schneider

Reputation: 7449

You need an event subscriber instead of an event listener.

You'd change the service tag to doctrine.event_subscriber, and your class should implement Doctrine\Common\EventSubscriber. You need to define a getSubscribedEvents to satisfy EventSubscriber which returns an array of events you want to subscribe to.

ex

<?php

namespace Company\YourBundle\Listener;

use Doctrine\Common\EventArgs;
use Doctrine\Common\EventSubscriber;

class YourListener implements EventSubscriber
{
    public function getSubscribedEvents()
    {
        return array('prePersist', 'onFlush');
    }

    public function prePersist(EventArgs $args)
    {

    }

    public function onFlush(EventArgs $args)
    {

    }
}

Upvotes: 7

Related Questions