【问题标题】:Symfony3 / Doctrine - Multiple file uploadSymfony3 / Doctrine - 多文件上传
【发布时间】:2017-08-04 16:03:47
【问题描述】:

我正在尝试学习本教程https://symfony.com/doc/current/controller/upload_file.html,它很好用,但我需要上传多个文件。

现在我有 2 个相关的表 Products 和 ProductImages。 我知道如何在控制器中上传多个文件,但我希望它在教义中,我认为它更干净和专业。

所以我尝试了很多与 foreach 的组合,但总是出错:

“Web\BaseBundle\Entity\ProductImages”类型的预期参数, 给出“Symfony\Component\HttpFoundation\File\UploadedFile”

我的实体:

产品:

class Product
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    ///

    /**
     * @ORM\OneToMany(targetEntity="Web\BaseBundle\Entity\ProductImages", mappedBy="product", cascade={"persist", "remove"})
     * @ORM\OrderBy({"id" = "ASC"})
     *
     */
    private $images;

    public function __toString(): string
    {
        return $this->getTitle();
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->images = new \Doctrine\Common\Collections\ArrayCollection();
    }

    ////

    /**
     * Add image
     *
     * @param \Web\BaseBundle\Entity\ProductImages $image
     *
     * @return Product
     */
    public function addImage(\Web\BaseBundle\Entity\ProductImages $image)
    {
        $this->images[] = $image;

        return $this;
    }

    /**
     * Remove image
     *
     * @param \Web\BaseBundle\Entity\ProductImages $image
     */
    public function removeImage(\Web\BaseBundle\Entity\ProductImages $image)
    {
        $this->images->removeElement($image);
    }

    /**
     * Get images
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getImages()
    {
        return $this->images;
    }
}

产品图片:

class ProductImages
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     *
     * @ORM\Column(name="image", type="string", length=255, nullable=true)
     * @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
     * @Assert\File(mimeTypes={ "image/jpeg", "image/jpg", "image/png" })
     */
    private $image;


    ///


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    ///

    /**
     * Set image
     *
     * @param string $image
     *
     * @return ProductImages
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

    /**
     * Get image
     *
     * @return string
     */
    public function getImage()
    {
        return $this->image;
    }

}

文件上传器:

class FileUploader
{
    private $targetDir;

    public function __construct($targetDir)
    {
        $this->targetDir = $targetDir;
    }

    public function upload(UploadedFile $file)
    {
        $fileName = md5(uniqid()).'.'.$file->guessExtension();

        $file->move($this->getTargetDir(), $fileName);

        return $fileName;
    }

    public function getTargetDir()
    {
        return $this->targetDir;
    }
}

文件上传监听器:

class FileUploadListener
{
    private $uploader;

    public function __construct(FileUploader $uploader)
    {
        $this->uploader = $uploader;
    }

    public function prePersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();

        $this->uploadFile($entity);
    }

    public function preUpdate(PreUpdateEventArgs $args)
    {
        $entity = $args->getEntity();

        $this->uploadFile($entity);
    }

    private function uploadFile($entity)
    {

        //Tried a lot of combinations with foreach, but not working :(        

        if (!$entity instanceof Product) {
            return;
        }

        $file = $entity->getImages();

        // only upload new files
        if (!$file instanceof UploadedFile) {
            return;
        }

        $fileName = $this->uploader->upload($file);
        $entity->setImages($fileName);
    }
}

有人可以帮我解决这个问题吗? 谢谢

【问题讨论】:

  • 我想,问题只出在uploadFile函数上。 :(
  • 看起来没人知道怎么做。好的

标签: symfony doctrine-orm


【解决方案1】:

我自己修的……

声明的温度。 Product 实体中文件的变量

private $files;

public function __construct()
{
    $this->files = new \Doctrine\Common\Collections\ArrayCollection();
}

public function setFiles($files)
{
    $this->files = $files;

    return $this;
}

public function getFiles()
{
    return $this->files;
}

更新uploadFile函数:

private function uploadFile($entity)
{
    if (!$entity instanceof Product) {
        return;
    }

    $files = $entity->getFiles();

    foreach ($files as $file) {
        if (!$file instanceof UploadedFile) {
            return;
        }
        $fileName = $this->uploader->upload($file);
        $productImage = new ProductImages();
        $productImage->setImage($fileName);
        $productImage->setProduct($entity);
        $entity->addImage($productImage);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2016-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多