0

I create a subscriber and register a new service, what I do : https://gist.github.com/Draeli/2c591c16409a5664ae58

<?php
namespace My\BlogBundle\Listener;

use Doctrine\ORM\Events;
use Symfony\Component\DependencyInjection\ContainerInterface;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

class BlogArticleDetailListener implements EventSubscriberInterface
{

    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    static public function getSubscribedEvents()
    {
        return array(
            'doctrine.event_subscriber' => array(
                array(Events::prePersist, 0),
                array(Events::preUpdate, 0),
            ),
        );
    }

    public function prePersist(FilterResponseEvent $event)
    {
        var_dump('prePersist');die;
    }

    public function preUpdate(FilterResponseEvent $event)
    {
        var_dump('preUpdate');die;
    }
}

services:
    my_blog.listener.blog_article_detail:
        class: My\BlogBundle\Listener\BlogArticleDetailListener
        arguments: ["@service_container"]
        tags:
            - { name: kernel.event_subscriber }

But in this case methods prePersist and preUpdate in spite of I persist objects, like if Doctrine didn't dispatch. Someone know what is strong in what I do ?

(For explanation, now I register only doctrine event but after I will register more from same place)

Draeli
  • 151
  • 2
  • 13

1 Answers1

1

Service

At present the listener is listening to event being dispatched using the Symfony kernel event dispatcher whereas you should be listening to the Doctrine event dispatcher.

Your service...

services:
    my_blog.listener.blog_article_detail:
        class: My\BlogBundle\Listener\BlogArticleDetailListener
        arguments: ["@service_container"]
        tags:
            - { name: kernel.event_subscriber }

Should have the tag doctrine.event_subscriber rather than the kernel.

Subscriber

As you are creating a Doctrine subscriber rather than a Symfony kernel subscriber you should implementing \Doctrine\Common\EventSubscriber, meaning the getSubscribedEvents method shouldn't be static.

Currently your subscriber is listening for an event called doctrine.event_subscriber rather than listening to the Doctrine events.

You should actually just be listening to the Doctrine events using...

public function getSubscribedEvents()
{
    return array(
        Events::prePersist,    // I'm not sure about setting
        Events::preUpdate,     // the priorities to be honest
    );
}

With the Doctrine events you won't be getting FilterResponseEvent as Doctrine dispatches with specific argument objects (Doctrine\Common\Persistence\Event\LifecycleEventArgs mostly) on events. For more info on the arguments (and the events) you can view the documentation.

qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • It was my first approch and works but this listener is suppose to be for one kind of object and not only register doctrine event and when I try to modify my code all fall :( – Draeli Jul 19 '14 at 16:51
  • I don't get you. Are you trying to combine doctrine and Symfony events in the same class? – qooplmao Jul 19 '14 at 17:09
  • I try to do something like that yes : http://symfony.com/doc/current/components/event_dispatcher/introduction.html#using-event-subscribers – Draeli Jul 19 '14 at 17:16
  • I don't think you can combine the 2 different event dispatchers with a subscriber. The thing you have linked to is just listening to the Symfony dispatcher. – qooplmao Jul 19 '14 at 17:42