Symfony 4-不允许“ Symfony \ Component \ HttpFoundation \ File \ File”的VichUploader序列化

时间:2019-09-08 18:05:48

标签: php file symfony serialization vichuploaderbundle

我有一个带有“ FeuilleConge”实体(带有VichUploader)的symfony 4项目,以及一个与“ FeuilleConge”实体相关的User实体和一个Absence实体。

当我提交缺勤时,我也会在用户-> getFeuilleConge()上保留“ FeuilleConge”。

但是当我提交缺席(与FeuilleConge实体一起)时,出现此错误:

  

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

我试图向我的FeuilleConge和Absence实体添加serialize()和unserialize()方法,如下所示:

Absence.php:

/**
 * @ORM\Entity(repositoryClass="App\Repository\AbsenceRepository")
 * @ORM\HasLifecycleCallbacks()
 * @Vich\Uploadable
 */
class Absence
{

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\FeuilleConge", inversedBy="absences", cascade={"persist", "remove"})
     */
    private $feuilleConge;

    /** @see \Serializable::serialize() */
    public function serialize()
    {
        return serialize(array(
            $this->id,

        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized)
    {
        list(
            $this->id,

        ) = unserialize($serialized);
    }

}

FeuilleConge.php:

<?php

namespace App\Entity;

use Serializable;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\File\File;
use Doctrine\Common\Collections\ArrayCollection;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Vich\UploaderBundle\Mapping\Annotation\Uploadable;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @ORM\Entity(repositoryClass="App\Repository\FeuilleCongeRepository")
 * @Vich\Uploadable
 */
class FeuilleConge
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="feuilleCongeName", type="string", length=255, nullable=true)
     */
    private $feuilleCongeName;

    /**
     * @Vich\UploadableField(mapping="feuillesConge", fileNameProperty="feuilleCongeName")
     * @var File
     */
    private $feuilleCongeFile;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @var \DateTime
     */
    private $updatedAt;

    /**
     * @ORM\OneToOne(targetEntity="App\Entity\User", mappedBy="feuilleConge", cascade={"persist", "remove"})
     */
    private $user;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Absence", mappedBy="feuilleConge")
     */
    private $absences;

    public function __construct()
    {
        $this->absences = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }


    /**
     * @param File|UploadedFile $feuilleConge
     *
     * @return FeuilleConge
     */
    public function setFeuilleCongeFile(File $feuilleCongeFile = null)
    {
        $this->feuilleCongeFile = $feuilleCongeFile;

        if ($feuilleCongeFile) {
            $this->updatedAt = new \DateTimeImmutable();
        }

        return $this;
    }

    /**
     * @return File|null
     */
    public function getFeuilleCongeFile()
    {
        return $this->feuilleCongeFile;
    }


    /**
     * Set urlPaquet
     *
     * @param string $urlPaquet
     *
     * @return FeuilleConge
     */
    public function setFeuilleCongeName($feuilleCongeName)
    {
        $this->feuilleCongeName = $feuilleCongeName;

        return $this;
    }

    /**
     * Get urlPaquet
     *
     * @return string|null
     */
    public function getFeuilleCongeName()
    {
        return $this->feuilleCongeName;
    }

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return FeuilleConge
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        // set (or unset) the owning side of the relation if necessary
        $newFeuilleConge = $user === null ? null : $this;
        if ($newFeuilleConge !== $user->getFeuilleConge()) {
            $user->setFeuilleConge($newFeuilleConge);
        }

        return $this;
    }

    /**
     * @return Collection|Absence[]
     */
    public function getAbsences(): Collection
    {
        return $this->absences;
    }

    public function addAbsence(Absence $absence): self
    {
        if (!$this->absences->contains($absence)) {
            $this->absences[] = $absence;
            $absence->setFeuilleConge($this);
        }

        return $this;
    }

    public function removeAbsence(Absence $absence): self
    {
        if ($this->absences->contains($absence)) {
            $this->absences->removeElement($absence);
            // set the owning side to null (unless already changed)
            if ($absence->getFeuilleConge() === $this) {
                $absence->setFeuilleConge(null);
            }
        }

        return $this;
    }

    /** @see \Serializable::serialize() */
    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->feuilleCongeName,

        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized)
    {
        list(
            $this->id,

        ) = unserialize($serialized);
    }
}

我在用户实体上添加了相同的方法。 但是问题是一样的。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您需要在User类上添加序列化方法,因为您的问题是由于Symfony试图对其进行序列化。然后,仅因为Symfony尝试序列化整个User对象(需要避免)而将您的Absence类序列化