@@ -17,6 +17,8 @@ Doctrine defines two types of objects that can listen to Doctrine events:
17
17
listeners and subscribers. Both are very similar, but listeners are a bit
18
18
more straightforward. For more, see `The Event System `_ on Doctrine's website.
19
19
20
+ The Doctrine website also explains all existing events that can be listened to.
21
+
20
22
Configuring the Listener/Subscriber
21
23
-----------------------------------
22
24
@@ -120,7 +122,9 @@ Creating the Listener Class
120
122
121
123
In the previous example, a service ``my.listener `` was configured as a Doctrine
122
124
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
124
128
125
129
// src/Acme/SearchBundle/EventListener/SearchIndexer.php
126
130
namespace Acme\SearchBundle\EventListener;
@@ -152,4 +156,62 @@ specific type of entity (e.g. a ``Product`` entity but not a ``BlogPost``
152
156
entity), you should check for the class name of the entity in your method
153
157
(as shown above).
154
158
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