Fix bugs in Managing Files and Images code#744
Fix bugs in Managing Files and Images code#744dunglas merged 1 commit intoapi-platform:2.4from baudev:baudev-file-upload
Conversation
|
Thanks @baudev.
A PR to update the back part would be great.
I definitely agree. Our long term plan is to support this directly in API Platform (without needing such docs, and without requiring the external bundle). |
|
@dunglas However, there is a problem with my solution. To better understand, this is the code I had to put in place in order for the administration interface to work: public function __invoke(Request $data): Image
{
$mediaObject= new MediaObject(); // we create a mediaObject entity
// we try getting a mediaObject entity from the mediaObject form
$form = $this->factory->create(MediaObjectType::class, $mediaObject);
$form->handleRequest($data);
if ($form->isSubmitted() && $form->isValid() && $mediaObject->file != null) {
$em = $this->doctrine->getManager();
$em->persist($mediaObject); // we store the image in the database
$em->flush();
// Prevent the serialization of the file property
$mediaObject->file = null;
return $mediaObject;
}
// if the file is null, then we return the last inserted Image entity
return $this->mediaObjectManager->getLastInsert();
}As I explained above, this line However, the behavior may seem completely strange and surprising if you don't use the administration interface... Indeed, everyone will wonder why we return the last saved entity when the file attribute is null! So what do we do? In the meantime I wait before making a PR of the back documentation.... |
As mentioned in the issue api-platform/admin#146, the code proposed in the documention to handle file upload is not fully working.
In this PR, I removed the useless
<FunctionField>component. Indeed, I see no interest using it.Other modifications don't require to be explained (minor fixes such as
valueinstead ofvalue[0]).Nevertheless, the following lines (picked from the doc) need to be discussed :
This part of code is responsible of adding a new request to upload the file. When you click on the creation button, it first sends the file to
/images/upload. Then, the usual request/images(the one for each creation entity) is executed.It could be resumed with the following graph:
It's really important to understand that there are two requests when clicking on the creation button.
However, the documentation of how to handle file upload in the API Platform back is not corresponding to the strategy used in the admin interface.
Let's see why in different points:
If we use the previous code in the admin interface with the Image class, an error will occur while doing the second request as the file attribute will be null (because the code only edits the rendering of the
contentUrlattribute).We must remove the
@Assert\NotNull()annotation.Now, if we try again in the admin interface to upload a file again, two instances of the Image class will be saved in the database (as there are two requests). The first one is the one which uploads the file as desired but the second one is empty. Also, the user is redirected to the page of the last image instance (the empty one, so).
We must then change the condition to:
and replace:
by:
It's only now that the whole photo upload system works! 🎉
Do I need to do a PR to modify the documentation of this page or just add a note in the admin documentation part?
Maybe an easier solution could be proposed to handle file upload?
I got many difficulties to explain the problem in this PR in writing. Don't hesitate to ask me questions if I haven't been clear on some points.