Temporarly disabling a doctrine entity listener

I use doctrine entity listeners to send mail whenever an entity is created. I want to be able to disable this event listener when performing certain operations (fixtures are mailbombing me as soon as I try to populate my database with fake entities).

I tried to disable the listener by using the clear method of the EntityListenerResolver class without success.

Here is my listener configuration:

services:
    mail_on_create_document_listener:
        class: AppEventListenerMailOnCreateDocumentListener
        tags:
            -
                name: 'doctrine.orm.entity_listener'
                event: 'postPersist'
                entity: 'AppEntityDocument'

I try to disable the listener with this code:

<?php

namespace AppDataFixtures;

use DoctrineBundleFixturesBundleFixture;
use DoctrineCommonPersistenceObjectManager;

class DebugFixture extends Fixture implements FixtureGroupInterface
{
    public function load(ObjectManager $manager)
    {
        $manager->getConfiguration()->getEntityListenerResolver()->clear(MailOnCreateDocumentListener::class);

        dump($manager->getConfiguration()->getEntityListenerResolver());
    }
}

Here is the resulting dump of the EntityListenerResolver

^ DoctrineBundleDoctrineBundleMappingContainerEntityListenerResolver^ {#1233
  -container: SymfonyComponentDependencyInjectionArgumentServiceLocator^ {#5278
    -factory: SymfonyComponentDependencyInjectionContainer::getService($registry, string $id, ?string $method, $load)^ {#283
      this: ContainerHYiq7ExsrcApp_KernelDevDebugContainer {#4404 …}
    }
    -serviceMap: array:1 [
      "mail_on_create_document_listener" => array:4 [
        0 => "privates"
        1 => "mail_on_create_document_listener"
        2 => "getMailOnCreateDocumentListenerService.php"
        3 => true
      ]
    ]
    -serviceTypes: array:1 [
      "mail_on_create_document_listener" => "?"
    ]
    -externalId: null
    -container: null
    -factories: array:1 [
      "mail_on_create_document_listener" => array:4 [
        0 => "privates"
        1 => "mail_on_create_document_listener"
        2 => "getMailOnCreateDocumentListenerService.php"
        3 => true
      ]
    ]
    -loading: []
    -providedTypes: null
  }
  -instances: []
  -serviceIds: array:1 [
    "AppEventListenerMailOnCreateDocumentListener" => "mail_on_create_document_listener"
  ]
}

Reading the code, it seems that the clear from EntityListenerResolver affects the instance part, but not the serviceIds.

How does this clear method is supposed to work?
Is my service declaration wrong?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *