Description
Hi all,
excuse me if I'm not posting this with the right title/tags.
Doc: https://symfony.com/doc/current/controller/upload_file.html
I'm implementing basic file upload, but I find one of the steps unnecessary. I'm talking about this tip:
When creating a form to edit an already persisted item, the file form type still expects a :class:
Symfony\\Component\\HttpFoundation\\File\\File
instance. As the persisted entity now contains only the relative file path, you first have to concatenate the configured upload path with the stored filename and create a new File class:
use Symfony\Component\HttpFoundation\File\File;
// ...
$product->setBrochureFilename(
new File($this->getParameter('brochures_directory').'/'.$product->getBrochureFilename())
);
The form type has a field brochure
like so:
->add('brochure', FileType::class, [
'label' => 'Brochure (PDF file)',
// unmapped means that this field is not associated to any entity property
'mapped' => false,
which is not mapped. So calling:
$product->setBrochureFilename(
new File($this->getParameter('brochures_directory').'/'.$product->getBrochureFilename())
);
will not do anything related to the FormType
, because this field is not in the form type declaration. On the other hand $product->setBrochureFilename()
actually expects string
. This probably will work because File
extends from SplFileInfo
which has SplFileInfo::__toString — Returns the path to the file as a string
.
This will cause a problem, because SplFileInfo::__toString
return the whole path, so after persisting the entity brochureFilename
field will be updated with the full path, instead of just the filename.
So basically without the $product->setBrochureFilename()
everything works.