Skip to content

[cookbook] add description of doctrine event subscriber #2301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/event_dispatcher/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ can be the way to go, especially for optional dependencies.
.. index::
single: Event Dispatcher; Event subscribers

.. _event_dispatcher-using-event-subscribers:

Using Event Subscribers
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
66 changes: 64 additions & 2 deletions cookbook/doctrine/event_listeners_subscribers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Doctrine defines two types of objects that can listen to Doctrine events:
listeners and subscribers. Both are very similar, but listeners are a bit
more straightforward. For more, see `The Event System`_ on Doctrine's website.

The Doctrine website also explains all existing events that can be listened to.

Configuring the Listener/Subscriber
-----------------------------------

Expand Down Expand Up @@ -120,7 +122,9 @@ Creating the Listener Class

In the previous example, a service ``my.listener`` was configured as a Doctrine
listener on the event ``postPersist``. That class behind that service must have
a ``postPersist`` method, which will be called when the event is thrown::
a ``postPersist`` method, which will be called when the event is thrown.

.. code-block::php

// src/Acme/SearchBundle/EventListener/SearchIndexer.php
namespace Acme\SearchBundle\EventListener;
Expand Down Expand Up @@ -152,4 +156,62 @@ specific type of entity (e.g. a ``Product`` entity but not a ``BlogPost``
entity), you should check for the class name of the entity in your method
(as shown above).

.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/events.html
Creating the Subscriber Class
-----------------------------

A doctrine event subscriber must implement the ``Doctrine\Common\EventSubscriber``
interface and an event method for each event it subscribes to.

.. code-block::php

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add .. code-block::php before this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// src/Acme/SearchBundle/EventListener/SearchIndexerSubscriber.php
namespace Acme\SearchBundle\EventListener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
// for doctrine 2.4: Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Acme\StoreBundle\Entity\Product;

class SearchIndexerSubscriber implements EventSubscriber
{
public function getSubscribedEvents()
{
return array(
'postPersist',
'postUpdate',
);
}

public function postUpdate(LifecycleEventArgs $args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add new lines before each method name

{
$this->index($args);
}

public function postPersist(LifecycleEventArgs $args)
{
$this->index($args);
}

public function index(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();

// perhaps you only want to act on some "Product" entity
if ($entity instanceof Product) {
// ... do something with the Product
}
}
}

.. tip::

Doctrine event subscribers can not return a flexible array of methods to
call for the events like the :ref:`Symfony event subscriber <event_dispatcher-using-event-subscribers>`
can do. Doctrine event subscribers must return a simple array of the event
names they subscribe to. Doctrine will then expect methods on the subscriber
with the names of the subscribed events, just as when using an event listener.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should contribute this to the Doctrine Common documentation about the EventManager instead and link it from the Symfony doc. Let's improve the doc of the Doctrine project as well :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can try an update of the event listener doc in doctrine as well. but i still think that at least this tip belongs here rather than into the doctrine doc.
the doctrine doc already has an example of event listeners, but not event subscribers i think. i thought it does make sense to have the minimal examples here in the symfony doc too, to see how it works and then follow the link for details. how much of this cookbook entry would you want to remove?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, the doctrine doc should have an example of subscribers IMO


For a full reference, see chapter `The Event System` in the doctrine documentation.

.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html