From f6068c9b287d5176aa822a7c623d1d06927d8d70 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 21 May 2016 19:39:24 +0200 Subject: [PATCH] Few fixes after testing the code --- cookbook/controller/upload_file.rst | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/cookbook/controller/upload_file.rst b/cookbook/controller/upload_file.rst index 99fcc6cbcfe..3613bcac2e7 100644 --- a/cookbook/controller/upload_file.rst +++ b/cookbook/controller/upload_file.rst @@ -130,7 +130,7 @@ Finally, you need to update the code of the controller that handles the form:: $form = $this->createForm(new ProductType(), $product); $form->handleRequest($request); - if ($form->isValid()) { + if ($form->isSubmitted() && $form->isValid()) { // $file stores the uploaded PDF file /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ $file = $product->getBrochure(); @@ -210,7 +210,7 @@ To avoid logic in controllers, making them big, you can extract the upload logic to a seperate service:: // src/AppBundle/FileUploader.php - namespace AppBundle\FileUploader; + namespace AppBundle; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -304,7 +304,9 @@ automatically upload the file when persisting the entity:: // src/AppBundle/EventListener/BrochureUploadListener.php namespace AppBundle\EventListener; + use Symfony\Component\HttpFoundation\File\UploadedFile; use Doctrine\ORM\Event\LifecycleEventArgs; + use Doctrine\ORM\Event\PreUpdateEventArgs; use AppBundle\Entity\Product; use AppBundle\FileUploader; @@ -324,7 +326,7 @@ automatically upload the file when persisting the entity:: $this->uploadFile($entity); } - public function preUpdate(LifecycleEventArs $args) + public function preUpdate(PreUpdateEventArgs $args) { $entity = $args->getEntity(); @@ -364,6 +366,7 @@ Now, register this class as a Doctrine listener: arguments: ['@app.brochure_uploader'] tags: - { name: doctrine.event_listener, event: prePersist } + - { name: doctrine.event_listener, event: preUpdate } .. code-block:: xml @@ -382,6 +385,7 @@ Now, register this class as a Doctrine listener: + @@ -398,6 +402,9 @@ Now, register this class as a Doctrine listener: $definition->addTag('doctrine.event_listener', array( 'event' => 'prePersist', )); + $definition->addTag('doctrine.event_listener', array( + 'event' => 'preUpdate', + )); $container->setDefinition('app.doctrine_brochure_listener', $definition); This listeners is now automatically executed when persisting a new Product