Skip to content

Commit 508e2e6

Browse files
committed
Merge pull request #2301 from dbu/explain-doctrine-event-subscribers
[cookbook] add description of doctrine event subscriber
2 parents e4e9bb8 + 0924f5c commit 508e2e6

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

components/event_dispatcher/introduction.rst

+2
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ can be the way to go, especially for optional dependencies.
380380
.. index::
381381
single: Event Dispatcher; Event subscribers
382382

383+
.. _event_dispatcher-using-event-subscribers:
384+
383385
Using Event Subscribers
384386
~~~~~~~~~~~~~~~~~~~~~~~
385387

cookbook/doctrine/event_listeners_subscribers.rst

+64-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Doctrine defines two types of objects that can listen to Doctrine events:
1717
listeners and subscribers. Both are very similar, but listeners are a bit
1818
more straightforward. For more, see `The Event System`_ on Doctrine's website.
1919

20+
The Doctrine website also explains all existing events that can be listened to.
21+
2022
Configuring the Listener/Subscriber
2123
-----------------------------------
2224

@@ -120,7 +122,9 @@ Creating the Listener Class
120122

121123
In the previous example, a service ``my.listener`` was configured as a Doctrine
122124
listener on the event ``postPersist``. That class behind that service must have
123-
a ``postPersist`` method, which will be called when the event is thrown::
125+
a ``postPersist`` method, which will be called when the event is thrown.
126+
127+
.. code-block::php
124128
125129
// src/Acme/SearchBundle/EventListener/SearchIndexer.php
126130
namespace Acme\SearchBundle\EventListener;
@@ -152,4 +156,62 @@ specific type of entity (e.g. a ``Product`` entity but not a ``BlogPost``
152156
entity), you should check for the class name of the entity in your method
153157
(as shown above).
154158

155-
.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/events.html
159+
Creating the Subscriber Class
160+
-----------------------------
161+
162+
A doctrine event subscriber must implement the ``Doctrine\Common\EventSubscriber``
163+
interface and an event method for each event it subscribes to.
164+
165+
.. code-block::php
166+
167+
// src/Acme/SearchBundle/EventListener/SearchIndexerSubscriber.php
168+
namespace Acme\SearchBundle\EventListener;
169+
170+
use Doctrine\Common\EventSubscriber;
171+
use Doctrine\ORM\Event\LifecycleEventArgs;
172+
// for doctrine 2.4: Doctrine\Common\Persistence\Event\LifecycleEventArgs;
173+
use Acme\StoreBundle\Entity\Product;
174+
175+
class SearchIndexerSubscriber implements EventSubscriber
176+
{
177+
public function getSubscribedEvents()
178+
{
179+
return array(
180+
'postPersist',
181+
'postUpdate',
182+
);
183+
}
184+
185+
public function postUpdate(LifecycleEventArgs $args)
186+
{
187+
$this->index($args);
188+
}
189+
190+
public function postPersist(LifecycleEventArgs $args)
191+
{
192+
$this->index($args);
193+
}
194+
195+
public function index(LifecycleEventArgs $args)
196+
{
197+
$entity = $args->getEntity();
198+
$entityManager = $args->getEntityManager();
199+
200+
// perhaps you only want to act on some "Product" entity
201+
if ($entity instanceof Product) {
202+
// ... do something with the Product
203+
}
204+
}
205+
}
206+
207+
.. tip::
208+
209+
Doctrine event subscribers can not return a flexible array of methods to
210+
call for the events like the :ref:`Symfony event subscriber <event_dispatcher-using-event-subscribers>`
211+
can do. Doctrine event subscribers must return a simple array of the event
212+
names they subscribe to. Doctrine will then expect methods on the subscriber
213+
with the names of the subscribed events, just as when using an event listener.
214+
215+
For a full reference, see chapter `The Event System` in the doctrine documentation.
216+
217+
.. _`The Event System`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html

0 commit comments

Comments
 (0)