doctrine-ormsoft-deletesymfony-2.1

Listener "SoftDeleteableListener" was not added to the EventManager


I followed the this example to test softdeletable extension on my project running Symfony 2.1.0-DEV.

I configured my config.yml like below:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    auto_mapping: true
    filters:
          softdeleteable:
            class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
            enabled: true
    mappings:
        translatable:
            type: annotation
            alias: Gedmo
            prefix: Gedmo\Translatable\Entity
            # make sure vendor library location is correct
            dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
        loggable:
            type: annotation
            alias: Gedmo
            prefix: Gedmo\Loggable\Entity
            dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
        tree:
            type: annotation
            alias: Gedmo
            prefix: Gedmo\Tree\Entity
            dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"   

My controller action:

/**
 * @Route("/del", name="del_article")
 */
public function delAction() {
    $em = $this->getDoctrine()->getEntityManager();

    $article = $em->find('Article', 3);
    $em->remove($article);
            $em->flush();
    die('ok');
}

When I run the code, it always show the exception: Listener "SoftDeleteableListener" was not added to the EventManager!

After some time spent with debugging, I found that the class SoftDeleteableFilter has function getListener():

protected function getListener()
{
    if ($this->listener === null) {
        $em = $this->getEntityManager();
        $evm = $em->getEventManager();

        foreach ($evm->getListeners() as $listeners) {
            foreach ($listeners as $listener) {
                if ($listener instanceof SoftDeleteableListener) {
                    $this->listener = $listener;

                    break 2;
                }
            }
        }

        if ($this->listener === null) {
            throw new \RuntimeException('Listener "SoftDeleteableListener" was not added to the EventManager!');
        }
    }

    return $this->listener;
}

However $listeners property has no SoftDeleteableListener item, but it has other listeners, such as

Which are generated from loadClassMetadata. I think it might generate from my doctrine_extensions.yml service listener:

services:
    extension.listener:
        class: Infinitz\UserBundle\Listener\DoctrineExtensionListener
        calls:
            - [ setContainer, [ @service_container ] ]
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onLateKernelRequest, priority: -10 }
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
    gedmo.listener.tree:
        class: Gedmo\Tree\TreeListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]

    gedmo.listener.translatable:
        class: Gedmo\Translatable\TranslatableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]
            - [ setDefaultLocale, [ %locale% ] ]
            - [ setTranslationFallback, [ false ] ]

    gedmo.listener.timestampable:
        class: Gedmo\Timestampable\TimestampableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]

    gedmo.listener.sluggable:
        class: Gedmo\Sluggable\SluggableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]

    gedmo.listener.sortable:
        class: Gedmo\Sortable\SortableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]

    gedmo.listener.loggable:
        class: Gedmo\Loggable\LoggableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ @annotation_reader ] ]         

So I tried to add following:

gedmo.listener.softdeleteable:
    class: Gedmo\SoftDeleteable\SoftDeleteableListener
    tags:
        - { name: doctrine.event_subscriber, connection: default }
    calls:
        - [ setAnnotationReader, [ @annotation_reader ] ]

But it still shows Listener "SoftDeleteableListener" was not added to the EventManager!

Do I need to add an listener which instance of SoftDeleteableListener?


Solution

  • Couldn't solve problem because of unclear answer.

    To add softdeletable behaviour to your project add following lines to your config.yml

    orm
      ..
      filters:
        softdeleteable:
          class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
          enabled: true
    
    services:
      ..
      gedmo.listener.softdeleteable:
        class: Gedmo\SoftDeleteable\SoftDeleteableListener
        tags:
            - { name: doctrine.event_subscriber, connection: default }
        calls:
            - [ setAnnotationReader, [ '@annotation_reader' ] ]
    

    Btw, more complete discussion, which helped me, can be found: https://github.com/Atlantic18/DoctrineExtensions/issues/380