Symfony 7 Doctrine EventSubscriber not used

74 Views Asked by At

Trying to register a Doctrine EventSubscriber but it doesn't work.

(symfony 7)

Here's the Subscriber:

<?php

namespace App\EventSubscriber;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;

class LogSubscriber implements EventSubscriber
{
    public function getSubscribedEvents()
    {
        return [
            Events::postPersist,
            Events::postUpdate,
            Events::postRemove
        ];
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        dd($args);
    }

    public function postUpdate(LifecycleEventArgs $args)
    {
        dd($args);
    }

    public function postRemove(LifecycleEventArgs $args)
    {
        dd($args);
    }
}

and my services.yaml

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:

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.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
    App\EventSubscriber\LogSubscriber:
        tags:
            - { name: doctrine.orm.event_subscribe, connection: default }

I tried with 'doctrine.event_subscribe' but that did not work.

I don't really understand what's wrong, thanks in advance for the help.

1

There are 1 best solutions below

0
tlorens On BEST ANSWER

As @yolenoyer commented, the interface has been deprecated.

Preface your Listener with the new PHP 8 Attributes

<?php

declare(strict_types=1);

namespace App\EventListener;

use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostRemoveEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Doctrine\ORM\Event\PrePersistEventArgs;
use Doctrine\ORM\Event\PreRemoveEventArgs;
use Doctrine\ORM\Events;

#[AsDoctrineListener(event: Events::prePersist, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::postPersist, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::postRemove, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::postUpdate, priority: 0, connection: 'default')]
#[AsDoctrineListener(event: Events::preRemove, priority: 0, connection: 'default')]
class EventListener
{
    public function prePersist(PrePersistEventArgs $event): void
    {
        $this->logger->debug('-- PrePersistListener::PREPERSIST --');
    }

    public function postPersist(PostPersistEventArgs $event): void
    {
        $this->logger->debug('-- PostPersistListener::POSTPERSIST --');
    }

    public function postRemove(PostRemoveEventArgs $event): void
    {
        $this->logger->debug('-- PostRemoveListener::POSTREMOVE --');
    }

    public function postUpdate(PostUpdateEventArgs $event): void
    {
        $this->logger->debug('-- PostUpdateListener::POSTUPDATE --');
    }

    /**
     * You need to listen to preRemove if you use soft delete
     * from Doctrine extensions, because it prevents postRemove
     * from being called.
     */
    public function preRemove(PreRemoveEventArgs $event): void
    {
        $this->logger->debug('-- PreRemoveListener::PREREMOVE --');
    }
}