监听器“SoftDeleteableListener”未添加到EventManager中

时间:2012-07-02 22:23:01

标签: doctrine-orm soft-delete symfony-2.1

我在运行Symfony 2.1.0-DEV的项目上按this example测试softdeletable扩展名。

我配置了我的config.yml,如下所示:

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"   

我的控制器操作:

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

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

当我运行代码时,它总是显示异常:Listener "SoftDeleteableListener" was not added to the EventManager!

花了一些时间进行调试后,我发现课程SoftDeleteableFilter的功能为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;
}

但是$listeners属性没有SoftDeleteableListener项,但它有其他侦听器,例如

  • Gedmo \树\ TreeListener
  • Gedmo \可排序\ SortableListener
  • Gedmo \ Sluggable \ SluggableListener
  • Gedmo \可记录的\ LoggableListener
  • Gedmo \ Timestampable \ TimestampableListener
  • Gedmo \可翻译\ TranslatableListener

哪些是从loadClassMetadata生成的。我认为它可能来自我的doctrine_extensions.yml服务监听器:

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 ] ]         

所以我尝试添加以下内容:

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

但它仍显示Listener "SoftDeleteableListener" was not added to the EventManager!

我是否需要添加一个侦听器,其中包含哪个SoftDeleteableListener实例?

4 个答案:

答案 0 :(得分:19)

因答案不清楚而无法解决问题。

要向项目添加softdeletable行为,请在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 ] ]

顺便说一句,有关我的更完整的讨论可以找到:https://github.com/Atlantic18/DoctrineExtensions/issues/380

答案 1 :(得分:14)

请参阅:https://github.com/stof/StofDoctrineExtensionsBundle/blob/master/Resources/doc/index.rst#activate-the-extensions-you-want

将以下内容添加到config.yml以激活softdelete侦听器:

# app/config.yml
stof_doctrine_extensions:
    default_locale: %locale%
    orm:
        default:
            softdeleteable: true

答案 2 :(得分:0)

抱歉我粗心大意,因为我在文件底部的config.yml文件中覆盖了我的配置:

services:
    gedmo.listener.softdeleteable:
        class:        Gedmo\SoftDeleteable\SoftDeleteableListener

并没有正确配置.....现在问题已得到解决。

答案 3 :(得分:0)

如果您使用的是api平台 这是我的config/packages/doctrine.yml

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'

        # IMPORTANT: You MUST configure your server version,
        # either here or in the DATABASE_URL env var (see .env file)
        server_version: '12'
    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App
        filters:
            softdeleteable:
                class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                enabled: true

之后,将新服务添加到config.services.yml

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

最后将注释添加到entity

use Gedmo\Mapping\Annotation as Gedmo;

/**
* @ApiResource()
* @ORM\Entity(repositoryClass=StoreRepository::class)
* @ORM\Table(name="trn_stores")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class Store ...

就这样