Symfony2 Doctrine:Entity Listener,为什么只有preFlush工作?

时间:2022-09-15 20:12:23

At the begginig sorry for my poor english :)

在begginig对不起我可怜的英语:)

I have in app/config/services.yml:

我在app / config / services.yml中:

page_listener:
    class: AppBundle\Entity\PageListener
        arguments: [ "@service_container" ]
        tags:
            - { name: doctrine.orm.entity_listener }

in AppBundle\Resources\config\doctrine\Page.orm.yml:

entityListeners:
    PageListener:
        postFlush: [postFlush]
        preFlush: [preFlush]

in AppBundle\Entity\PageListener:

namespace AppBundle\Entity;

use Cocur\Slugify\Slugify;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;

class PageListener
{
    public function __construct($container){ /* nothing here */ }

    public function preFlush(Page $page, PreFlushEventArgs $event){ ... }

    public function postFlush(Page $page, PostFlushEventArgs $event){ ... }
}

Now when i do

现在当我这样做

$em->persist($page);
$em->flush();

preFlush do what have to do, but postFlush not event start :/ Even when i put exit, or something like that in postFlush nothing happens.

preFlush做了必须做的事情,但postFlush没有事件开始:/即使我把退出,或者类似于postFlush的东西都没有发生。

Do I forget something or what? Any suggestions?

我忘了什么或什么?有什么建议?

2 个解决方案

#1


I don't have full idea about it but still I know you need to define event when you are registering the event.

我对此并不完全了解,但我知道您在注册活动时需要定义事件。

  my.listener2:

    class: Acme\SearchBundle\EventListener\SearchIndexer2

    tags:
        - { name: doctrine.event_listener, event: postPersist, connection: default }

kindly refer this link:

请参考此链接:

http://inchoo.net/dev-talk/symfony2-event-listeners/

#2


It may depend on your Symfony or Doctrine version. Paste your composer.json

它可能取决于您的Symfony或Doctrine版本。粘贴你的composer.json

#1


I don't have full idea about it but still I know you need to define event when you are registering the event.

我对此并不完全了解,但我知道您在注册活动时需要定义事件。

  my.listener2:

    class: Acme\SearchBundle\EventListener\SearchIndexer2

    tags:
        - { name: doctrine.event_listener, event: postPersist, connection: default }

kindly refer this link:

请参考此链接:

http://inchoo.net/dev-talk/symfony2-event-listeners/

#2


It may depend on your Symfony or Doctrine version. Paste your composer.json

它可能取决于您的Symfony或Doctrine版本。粘贴你的composer.json