Symfony2: editing file upload

I am using a cookbook article from symfony.com to implement the file upload option for images.

Now I want to load other images into an object.

The default strategy for editing is: 1. Extract from the database 2. Enter the form 3. Persistent

Somehow this strategy no longer works when using file upload (doctrine does not execute events)

What else could I do to make picture articles editable?

+6
source share
2 answers

The cookbook does not process updates , in particular when only the file is changed.

In this case, the PreUpdate event PreUpdate not PreUpdate , so you need to fire $entity->preUpload() manually before $em->persist($entity) so that the file is processed anyway (preUpload will change $entity->path , so that )

+10
source

If you change only the download field, the life cycle does not start the download method. The cookbook reports the decision in the quote box, as shown below:

The PreUpdate and PostUpdate callbacks are only triggered if there is a change in one of the object fields that are saved. This means that by default, if you change only the $ file property, these events will not be triggered, since the property itself is not direct saved through the Doctrine. One solution would be to use an updated field that is saved in the Doctrine, and change them manually when changing the file.

add a dummy field for updating in the controller before saving the event, as duscussion suggests:

https://github.com/symfony/symfony-docs/pull/564

 public function setFile(UploadedFile $file) { $this->file = $file; $this->updatedAt = new \DateTime(); } 
+7
source

Source: https://habr.com/ru/post/912162/


All Articles