1

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.

Newbiedev
  • 141
  • 3
  • 20
  • Did you try to clear your cache as written in the VichUploader doc ? Don't forget to clear the cache once your entity is configured: php bin/console cache:clear – Athos Mar 17 '21 at 11:12
  • 1
    In order to use `__serialize()`/`__unserialize()` your class also needs to implement [the Serializable-interface](https://www.php.net/manual/en/class.serializable.php). Maybe that is the reason why it didn't work? – dbrumann Mar 17 '21 at 12:27
  • Hey there, thanks for the answers. Yes I've tried to clear cache. I also try to use the Serializable-interface, but I still have the same error. – Newbiedev Mar 17 '21 at 12:54
  • 1
    This is old but I think it is still relevant: [Why SplFileInfo cannot be serialized](https://stackoverflow.com/questions/6242821/why-serialization-of-splfileinfo-is-not-allowed). The Symfony File class extends SplFileInfo. The only thing you should really need to store in the entity is the path to wherever the file lives. – Cerad Mar 17 '21 at 13:29
  • @Newbiedev ~ did you find any solution for this issue? I've got the same error. – Praditha Apr 12 '22 at 07:46

0 Answers0