监听器“SoftDeleteableListener”未添加到EventManager

问题描述 投票:0回答:5

我按照这个示例在运行 Symfony 2.1.0-DEV 的项目上测试

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\Tree\TreeListener
  • Gedmo\Sortable\SortableListener
  • Gedmo\Sluggable\SluggableListener
  • Gedmo\Loggable\LoggableListener
  • Gedmo\Timestampable\TimestampableListener
  • Gedmo\可翻译\可翻译监听器

由 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 的实例)吗?

doctrine-orm soft-delete symfony-2.1
5个回答
30
投票

由于答案不明确而无法解决问题。

要将软删除行为添加到您的项目中,请将以下行添加到您的 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


16
投票

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

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

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

4
投票

如果您使用的是 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 ...

就是这样


0
投票

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

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

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


0
投票

从 Symfony 7 和 Doctrine 3 开始,这个设置对我有用:

# packages/doctrine.yaml
doctrine:
    orm:
        filters:
            softdeleteable:
                class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                enabled: true
# services.yaml
    Gedmo\Mapping\Driver\AttributeReader: ~
    Gedmo\SoftDeleteable\SoftDeleteableListener:
        class: Gedmo\SoftDeleteable\SoftDeleteableListener
        tags:
            - {name: doctrine.event_listener, event: 'onFlush'}
            - {name: doctrine.event_listener, event: 'loadClassMetadata'}
        calls:
            - setAnnotationReader: ['@Gedmo\Mapping\Driver\AttributeReader']
© www.soinside.com 2019 - 2024. All rights reserved.