You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<?phpnamespaceAcme\DemoBundle\Entity;
useDoctrine\ORM\MappingasORM;
useSymfony\Component\HttpFoundation\File\File;
useVich\UploaderBundle\Mapping\AnnotationasVich;
/** * @ORM\Entity * @Vich\Uploadable */classProduct
{
/** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */private$id;
// ..... other fields/** * NOTE: This is not a mapped field of entity metadata, just a simple property. * * @Vich\UploadableField(mapping="website_image", fileNameProperty="imageName") * * @var File */private$imageFile;
/** * @ORM\Column(type="string", length=255) * * @var string */private$imageName;
/** * @ORM\Column(type="datetime") * * @var \DateTime */private$updatedAt;
/** * If manually uploading a file (i.e. not using Symfony Form) ensure an instance * of 'UploadedFile' is injected into this setter to trigger the update. If this * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter * must be able to accept an instance of 'File' as the bundle will inject one here * during Doctrine hydration. * * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image * * @return Product */publicfunctionsetImageFile(File$image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine// otherwise the event listeners won't be called and the file is lost$this->updatedAt = new \DateTime('now');
}
return$this;
}
/** * @return File|null */publicfunctiongetImageFile()
{
return$this->imageFile;
}
/** * @param string $imageName * * @return Product */publicfunctionsetImageName($imageName)
{
$this->imageName = $imageName;
return$this;
}
/** * @return string|null */publicfunctiongetImageName()
{
return$this->imageName;
}
}
Ajouter un directory_namer permet par exemple d'avoir un sous dossier contenant l'id /images/websites/id/image.png au lieu de /images/websites/image.png
Créér un dossier dans votre bundle Acme\DemoBundle\Services;
Ajouter un fichier ImageDirectoryNamer.php dedans;