File tree 1 file changed +21
-3
lines changed
1 file changed +21
-3
lines changed Original file line number Diff line number Diff line change @@ -164,9 +164,10 @@ controller to specify the directory in which the brochures should be stored:
164
164
165
165
.. code-block :: yaml
166
166
167
- # app/config/parameters.yml
167
+ # app/config/config.yml
168
+
169
+ # ...
168
170
parameters :
169
- # ...
170
171
brochures_directory : ' %kernel.root_dir%/../web/uploads/brochures'
171
172
172
173
There are some important things to consider in the code of the above controller:
@@ -320,14 +321,31 @@ automatically upload the file when persisting the entity::
320
321
{
321
322
$entity = $args->getEntity();
322
323
324
+ $this->uploadFile($entity);
325
+ }
326
+
327
+ public function preUpdate(LifecycleEventArs $args)
328
+ {
329
+ $entity = $args->getEntity();
330
+
331
+ $this->uploadFile($entity);
332
+ }
333
+
334
+ private function uploadFile($entity)
335
+ {
323
336
// upload only works for Product entities
324
337
if (!$entity instanceof Product) {
325
338
return;
326
339
}
327
340
328
341
$file = $entity->getBrochure();
329
- $fileName = $this->uploader->upload($file);
330
342
343
+ // only upload new files
344
+ if (!$file instanceof UploadedFile) {
345
+ return;
346
+ }
347
+
348
+ $fileName = $this->uploader->upload($file);
331
349
$entity->setBrochure($fileName);
332
350
}
333
351
}
You can’t perform that action at this time.
0 commit comments