@@ -203,6 +203,21 @@ You can use the following code to link to the PDF brochure of an product:
203
203
View brochure (PDF)
204
204
</a>
205
205
206
+ .. tip ::
207
+
208
+ When creating a form to edit an already persisted item, the file form type
209
+ still expects a :class: `Symfony\\ Component\\ HttpFoundation\\ File\\ File `
210
+ instance. As the persisted entity now contains only the relative file path,
211
+ you first have to concatenate the configured upload path with the stored
212
+ filename and create a new ``File `` class::
213
+
214
+ use Symfony\Component\HttpFoundation\File\File;
215
+ // ...
216
+
217
+ $product->setBrochure(
218
+ new File($this->getParameter('brochures_directory').'/'.$product->getBrochure())
219
+ );
220
+
206
221
Creating an Uploader Service
207
222
----------------------------
208
223
@@ -411,4 +426,30 @@ This listeners is now automatically executed when persisting a new Product
411
426
entity. This way, you can remove everything related to uploading from the
412
427
controller.
413
428
429
+ .. tip ::
430
+
431
+ This listener can also create the ``File `` instance based on the path when
432
+ fetching entities from the database::
433
+
434
+ // ...
435
+ use Symfony\Component\HttpFoundation\File\File;
436
+
437
+ // ...
438
+ class BrochureUploadListener
439
+ {
440
+ // ...
441
+
442
+ public function postLoad(LifecycleEventArgs $args)
443
+ {
444
+ $entity = $args->getEntity();
445
+
446
+ $fileName = $entity->getBrochure();
447
+
448
+ $entity->setBrochure(new File($this->targetPath.'/'.$fileName));
449
+ }
450
+ }
451
+
452
+ After adding these lines, configure the listener to also listen for the
453
+ ``postLoad `` event.
454
+
414
455
.. _`VichUploaderBundle` : https://github.com/dustin10/VichUploaderBundle
0 commit comments