@@ -152,4 +152,56 @@ specific type of entity (e.g. a ``Product`` entity but not a ``BlogPost``
152
152
entity), you should check for the class name of the entity in your method
153
153
(as shown above).
154
154
155
+ Creating the Subscriber Class
156
+ -----------------------------
157
+
158
+ A doctrine event subscriber must implement the ``Doctrine\Common\EventSubscriber ``
159
+ interface and an event method for each event it subscribes to.
160
+
161
+
162
+ // src/Acme/SearchBundle/EventListener/SearchIndexerSubscriber.php
163
+ namespace Acme\S earchBundle\E ventListener;
164
+
165
+ use Doctrine\C ommon\E ventSubscriber;
166
+ use Doctrine\O RM\E vent\L ifecycleEventArgs;
167
+ use Acme\S toreBundle\E ntity\P roduct;
168
+
169
+ class SearchIndexerSubscriber implements EventSubscriber
170
+ {
171
+ public function getSubscribedEvents()
172
+ {
173
+ return array(
174
+ 'postPersist',
175
+ 'postUpdate',
176
+ );
177
+ }
178
+ public function postUpdate(LifecycleEventArgs $args)
179
+ {
180
+ $this->index($args);
181
+ }
182
+ public function postPersist(LifecycleEventArgs $args)
183
+ {
184
+ $this->index($args);
185
+ }
186
+ public function index(LifecycleEventArgs $args)
187
+ {
188
+ $entity = $args->getEntity();
189
+ $entityManager = $args->getEntityManager();
190
+
191
+ // perhaps you only want to act on some "Product" entity
192
+ if ($entity instanceof Product) {
193
+ // ... do something with the Product
194
+ }
195
+ }
196
+ }
197
+
198
+ .. hint ::
199
+
200
+ Doctrine event subscribers can not return a flexible array of methods to
201
+ call for the events like the :ref: `Symfony event subscriber <event_dispatcher-using-event-subscribers >`
202
+ can do. Doctrine event subscribers must return a simple array of the event
203
+ names they subscribe to. Doctrine will then expect methods on the subscriber
204
+ with the names of the subscribed events, just as when using an event listener.
205
+
206
+
155
207
.. _`The Event System` : http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/events.html
0 commit comments