Я делаю проект Symfony, в котором пользователь может загрузить изображение профиля. Я использую VichUploaderBundle для управления загрузкой изображений.
Я сделал это в своей сущности следующим образом:
/**
* @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;
}
Но когда мой пользователь хочет загрузить новую фотографию профиля, появляется следующая ошибка:
Сериализация Symfony\Component\HttpFoundation\File\File не разрешена.
Чего я не понимаю, так это того, что загрузка работает хорошо, новое изображение загружается правильно даже с ошибкой.
Чтобы исправить это, я попытался написать следующие методы searialize:
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;
}
Но даже с этим ошибка все еще сохраняется. У вас есть идеи о том, что не так? Большое спасибо.
__serialize()
/__unserialize()
, ваш класс также должен реализовать Serializable-interface< /а>. Может поэтому не получилось? - person dbrumann   schedule 17.03.2021