My problem is actually to upload file with vich. It work perfect with an other entity. But in my user's entity it doesnt work.
Vich uploader configuration :
vich_uploader:
db_driver: orm
mappings:
user_cover:
uri_prefix: /media/users
upload_destination: '%kernel.project_dir%/public/media/users'
namer: Vich\UploaderBundle\Naming\OrignameNamer
delete_on_update: true
delete_on_remove: true
play_media:
uri_prefix: /media/defis
upload_destination: '%kernel.project_dir%/public/media/defis'
namer: Vich\UploaderBundle\Naming\OrignameNamer
delete_on_update: true
delete_on_remove: true
The play_media mapping works perfectly but my problem come from user_cover
the part that relates in my user entity :
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
#[Groups(['read:User'] )]
private $CoverPath;
/**
* @var File|null
* @Assert\File(
* maxSize = "2048k",
* mimeTypes = {"image/jpeg", "image/png"},
* mimeTypesMessage = "Please upload a valid cover image: jpeg or png under 2048k")
* @Vich\UploadableField(mapping="play_media_test", fileNameProperty="CoverPath")
*/
private $file;
/**
* Set the value of file
*
* @param File|null $file
* @return User
*/
public function setFile(?File $file = null) :User
{
$this->file = $file;
if ($file instanceof File) {
$this->setUpdatedAt(new \DateTime());
}
return $this;
}
and my controller ( no checks yet ), in theory I think I'm not supposed to update the date because the setter is already doing it and no need to use the entity manager (I tried anyway), but ...:
public function __invoke(Request $request)
{
$user_data = $request->attributes->get('data');
$user_data->setFile($request->files->get('cover'));
$user_data->setUpdatedAt(new DateTime());
return $user_data;
}
and the operation configuartion :
'cover' => [
'method' => 'POST' ,
'path' => 'user/{id}/cover',
'deserialize' => false,
'controller' => PostCoverUserController::class,
'openapi_context' => [
'security' =>
[['bearerAuth' => []]],
'summary' => 'Update the user cover image ',
'requestBody' => [
'content' => [
'multipart/form-data' => [
'schema' => [
'type' => 'object',
'properties' => [
'cover' => [
'type' => 'string',
'format' => 'biniray'
]
]
]
]
]
]
]
],
I reread my code several times and I don't understand why the persistence is not working as expected? however my other upload works perfectly. the api send back the answer, dd works .. only the persistence fails.
thank you for your help
My Answer:
VichUploader is a PHP library that simplifies the process of handling file uploads in Symfony applications. It provides an easy way to manage file uploads, including handling file naming, storage, and retrieval. VichUploader also integrates seamlessly with Symfony's form handling capabilities, making it easy to create forms that allow users to upload files. Overall, VichUploader is a useful tool for managing file uploads in Symfony applications.
Rate this post
5 of 5 based on 9159 votesComments