@@ -293,4 +293,97 @@ Now you're ready to use this service in the controller::
293
293
// ...
294
294
}
295
295
296
+ Using a Doctrine Listener
297
+ -------------------------
298
+
299
+ If you are using Doctrine to store the Product entity, you can create a
300
+ :doc: `Doctrine listener </cookbook/doctrine/event_listeners_subscribers >` to
301
+ automatically upload the file when persisting the entity::
302
+
303
+ // src/AppBundle/EventListener/BrochureUploadListener.php
304
+ namespace AppBundle\EventListener;
305
+
306
+ use Doctrine\ORM\Event\LifecycleEventArgs;
307
+ use AppBundle\Entity\Product;
308
+ use AppBundle\FileUploader;
309
+
310
+ class BrochureUploadListener
311
+ {
312
+ private $uploader;
313
+
314
+ public function __construct(FileUploader $uploader)
315
+ {
316
+ $this->uploader = $uploader;
317
+ }
318
+
319
+ public function prePersist(LifecycleEventArgs $args)
320
+ {
321
+ $entity = $args->getEntity();
322
+
323
+ // upload only works for Product entities
324
+ if (!$entity instanceof Product) {
325
+ return;
326
+ }
327
+
328
+ $file = $entity->getBrochure();
329
+ $fileName = $this->uploader->upload($file);
330
+
331
+ $entity->setBrochure($fileName);
332
+ }
333
+ }
334
+
335
+ Now, register this class as a Doctrine listener:
336
+
337
+ .. configuration-block ::
338
+
339
+ .. code-block :: yaml
340
+
341
+ # app/config/services.yml
342
+ services :
343
+ # ...
344
+ app.doctrine_brochure_listener :
345
+ class : AppBundle\EventListener\BrochureUploadListener
346
+ arguments : ['@app.brochure_uploader']
347
+ tags :
348
+ - { name: doctrine.event_listener, event: prePersist }
349
+
350
+ .. code-block :: xml
351
+
352
+ <!-- app/config/config.xml -->
353
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
354
+ <container xmlns =" http://symfony.com/schema/dic/services"
355
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
356
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
357
+ http://symfony.com/schema/dic/services/services-1.0.xsd"
358
+ >
359
+ <!-- ... -->
360
+
361
+ <service id =" app.doctrine_brochure_listener"
362
+ class =" AppBundle\EventListener\BrochureUploaderListener"
363
+ >
364
+ <argument type =" service" id =" app.brochure_uploader" />
365
+
366
+ <tag name =" doctrine.event_listener" event =" prePersist" />
367
+ </service >
368
+ </container >
369
+
370
+ .. code-block :: php
371
+
372
+ // app/config/services.php
373
+ use Symfony\Component\DependencyInjection\Reference;
374
+
375
+ // ...
376
+ $definition = new Definition(
377
+ 'AppBundle\EventListener\BrochureUploaderListener',
378
+ array(new Reference('brochures_directory'))
379
+ );
380
+ $definition->addTag('doctrine.event_listener', array(
381
+ 'event' => 'prePersist',
382
+ ));
383
+ $container->setDefinition('app.doctrine_brochure_listener', $definition);
384
+
385
+ This listeners is now automatically executed when persisting a new Product
386
+ entity. This way, you can remove everything related to uploading from the
387
+ controller.
388
+
296
389
.. _`VichUploaderBundle` : https://github.com/dustin10/VichUploaderBundle
0 commit comments