15

我按照这个例子来测试softdeletable我运行 Symfony 2.1.0-DEV 的项目的扩展。

我配置了我的 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\Translatable\TranslatableListener

它们是从 loadClassMetadata 生成的。我认为它可能从我的学说扩展.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

4 回答 4

26

由于答案不清楚,无法解决问题。

要将 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

于 2012-12-25T08:10:52.677 回答
15

请参阅: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
于 2014-03-05T16:57:39.877 回答
1

如果你在 api-platform 这是我的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 ...

就是这样

于 2020-05-13T10:41:46.907 回答
0

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

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

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

于 2012-07-03T21:43:34.240 回答