symfony - 教义可上传: Multiple file upload on the same entity

标签 symfony doctrine-orm

我正在使用优秀的可上传教义扩展。我可以为每个实体上传一个文件,但如何在同一实体上上传两个不同的文件?

* @Gedmo\Uploadable(path="uploads/articles", appendNumber=true, filenameGenerator="SHA1")
class Article
{
    * @ORM\Column(name="photo", type="string", length=255)
    * @Gedmo\UploadableFilePath
    private $photo

    * @ORM\Column(name="pdf", type="string", length=255)
    * @Gedmo\UploadableFilePath
    private $pdf

在我的 Controller 上,我有:

$uploadableManager->markEntityToUpload($article, $article->getPhoto());
$uploadableManager->markEntityToUpload($article, $article->getPdf());

仅上传最后一个文件并将其保存到数据库中。我怎样才能做到这一点?

最佳答案

你可能混淆了一些东西。

您有包含两个字段的 Article 实体:照片和 pdf,但没有 $materia 实体。您可能应该将 $materia 更改为 $article。但这不起作用,因为 @Uploadable 无法为同一实体上传多个文件。

提示:使用VichUploaderBundle用于 Doctrine 文件上传处理

UPD:这是示例类。

<?php

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity
 * @ORM\Table(name="article")
 * @Vich\Uploadable
 */
class Article
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    // ..... other fields

    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     *
     * @Vich\UploadableField(mapping="article_photo", fileNameProperty="photoName")
     *
     * @var File
     */
    private $photoFile;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @var string
     */
    private $photoName;


    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     *
     * @Vich\UploadableField(mapping="article_pdf", fileNameProperty="pdfName")
     *
     * @var File
     */
    private $pdfFile;

    /**
     * @ORM\Column(type="string", length=255)
     *
     * @var string
     */
    private $pdfName;

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

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

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

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

        return $this;
    }

    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $photo
     *
     * @return Article
     */
    public function setPhotoFile(File $photo = null)
    {
        $this->photoFile = $photo;

        if ($photo) {
            // 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 \DateTime('now');
        }

        return $this;
    }

    /**
     * @return File
     */
    public function getPhotoFile()
    {
        return $this->photoFile;
    }

    /**
     * @param string $photoName
     *
     * @return Article
     */
    public function setPhotoName($photoName)
    {
        $this->photoName = $photoName;

        return $this;
    }

    /**
     * @return string
     */
    public function getPhotoName()
    {
        return $this->photoName;
    }


    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $pdf
     *
     * @return Article
     */
    public function setPdfFile(File $pdf = null)
    {
        $this->pdfFile = $pdf;

        if ($pdf) {
            // 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 \DateTime('now');
        }

        return $this;
    }

    /**
     * @return File
     */
    public function getPdfFile()
    {
        return $this->pdfFile;
    }

    /**
     * @param string $pdfName
     *
     * @return Article
     */
    public function setPdfName($pdfName)
    {
        $this->pdfName = $pdfName;

        return $this;
    }

    /**
     * @return string
     */
    public function getPdfName()
    {
        return $this->pdfName;
    }
}

你需要这样配置VichUploader:

# app/config/config.yml
vich_uploader:
    db_driver: orm

    mappings:
        article_photo:
            uri_prefix:         /images/articles/photos
            upload_destination: %kernel.root_dir%/../web/images/articles/photos
        article_pdf:
            uri_prefix:         /images/articles/pdfs
            upload_destination: %kernel.root_dir%/../web/images/articles/pdfs

要细心。您可能会对配置、映射、方法感到困惑……只需仔细、深思熟虑地阅读手册即可。 https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md

关于symfony - 教义可上传: Multiple file upload on the same entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22618027/

相关文章:

php - Symfony2 从具有多对多关系的倒置实体中获取对象

php - 如何在 Doctrine 2 中公开字段 "break lazy loading"?

php - 无法转换属性路径的值 "fechaReclamacion": datefmt_format: string '' is not numeric

symfony - FOSUserBundle 和 FR3DLdapBundle 缺少数据库列

symfony - 在学说查询中使用数组集合作为参数

来自数据库的 PHPUnit dataProvider

php - 如何在 Symfony2 Twig 模板中调用静态函数

php - 如何用 Twig 渲染字符串/数据库中的内容?

doctrine-orm - Doctrine 2 中不同所有者对象的多态关联

symfony2 多数据库