多文件上传Symfony 3.2

时间:2017-03-13 14:28:39

标签: php symfony doctrine-orm twig symfony-3.1

我想知道在SF3中处理多文件上传。实际上,在以前的版本中,您必须在实体上声明一个具有学说一对多关系的字段。 例如:

namespace ProjectBundle\Entity;
    /**
     * @ORM\OneToMany(targetEntity="ProjectBundle\Entity\SliderImage", mappedBy="project",orphanRemoval= true, cascade={"persist", "remove"})
     */
    private $sliderImages;

并在反向实体中:

namespace ProjectBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use PlatformBundle\Entity\Image;

/**
 * SliderImage
 *
 * @ORM\Table(name="slider_image")
 * @ORM\Entity(repositoryClass="ProjectBundle\Repository\SliderImageRepository")
 */
class SliderImage extends Image {

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

    /**
     *  @ORM\ManyToOne(targetEntity="ProjectBundle\Entity\Project", inversedBy="sliderImages")
     *  @ORM\JoinColumn(name="project_id", referencedColumnName="id")
     */
    private $project;

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

    protected function getUploadDir() {
        return 'uploads/images/projects/sliders';
    }

    function getProject() {
        return $this->project;
    }

    function setProject(Project $project) {
        $this->project = $project;
    }

}

最后,在FormType中你只需添加这个字段并在Twig方面用JavaScript管理:

        ->add('sliderImages', CollectionType::class, array(
            'entry_type' => SliderImageType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'label' => false,
            'by_reference' => false
        ))

但现在,根据symfony documentation,您必须声明一个这样的字段:

/**
 * @ORM\Column(type="string")
 *
 * @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
 * @Assert\File(mimeTypes={ "application/pdf" })
 */
private $brochure;

所以,就像没有关系,我怎么说我想要当前实体的多个小册子(在我的情况下,我想将多个图像链接到一个项目)。以及如何构建我的FormType? 我确定这是微不足道的,但我无法弄清楚

我尝试了一对一的关系并且工作得很好(为用户提供profilePicture)

0 个答案:

没有答案