微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

'Symfony\Component\HttpFoundation\File\File' 的序列化是不允许的

如何解决'Symfony\Component\HttpFoundation\File\File' 的序列化是不允许的

我正在制作一个 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' 的序列化不是 允许

我不明白的是,上传效果很好,即使出现错误,新图片也能正确上传

为了修复它,我尝试编写以下序列化方法

   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;
   }

但即使这样,错误仍然存​​在..你知道出了什么问题吗?非常感谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。