I'm making a Symfony project where user can upload a profile picture. I use VichUploaderBundle to manage image uploads.
I've made it in my entity like this :
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @Vich\Uploadable
*/
class User implements UserInterface
/**
* @Vich\UploadableField(mapping="image_profile", fileNameProperty="image_profile_name", size="image_profile_size")
*
* @var File
*/
private $image_profile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $image_profile_name;
/**
* @ORM\Column(type="integer")
*
* @var integer
*/
private $image_profile_size;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @var \DateTime
*/
private $updatedAt;
/**
* @return File
*/
public function getImageProfile()
{
return $this->image_profile;
}
/**
* @param File $image_profile
*/
public function setImageProfile(File $image_profile = null): void
{
$this->image_profile = $image_profile;
if (null !== $this->image_profile) {
// 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 \DateTimeImmutable();
}
}
/**
* @return string
*/
public function getImageProfileName(): string
{
return $this->image_profile_name;
}
/**
* @param string $image_profile_name
*/
public function setImageProfileName(string $image_profile_name): void
{
$this->image_profile_name = $image_profile_name;
}
/**
* @return \DateTime
*/
public function getUpdatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @param \DateTime $updatedAt
*/
public function setUpdatedAt(\DateTime $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
/**
* @return int
*/
public function getImageProfileSize(): int
{
return $this->image_profile_size;
}
/**
* @param int $image_profile_size
*/
public function setImageProfileSize(int $image_profile_size): void
{
$this->image_profile_size = $image_profile_size;
}
But when my user want to upload a new profile picte the following error appear :
Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed
What I don't understand is that the upload works well the new image is uploaded correctly even with the error.
To fix it I've tried to write the following searialize methods :
public function __serialize(): array
{
return [
'id' => $this->id,
'email' => $this->email,
'image_profile' => $this->image_profile,
];
}
public function __unserialize(array $serialized): User
{
$this->id = $serialized['id'];
$this->email = $serialized['email'];
$this->image_profile = $serialized['image_profile'];
return $this;
}
But even with this the error still persist.. Do you have any idea about what's wrong ? Thanks a lot.