2017-08-16 55 views
1

即时寻找多个上传。我也有工作上传,但仍然只有一个文件。我找不到解决我的问题的答案。我也尝试了很多教程,但没有为我工作。在他们的文档中我找不到答案。我该怎么办?symfony的多个上传vichuploadbundle

错误:

Expected argument of type "Symfony\Component\HttpFoundation\File\File", "array" given 

我的实体:

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

    /** 
    * @ORM\Column(type="string", length=50, nullable=true) 
    * @Assert\NotBlank() 
    * @Assert\Length(
    *  min=5, 
    *  minMessage="Title is too short!", 
    *  max=50, 
    *  maxMessage="Title is too long!" 
    *) 
    */ 
    private $title; 

    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User") 
    * @ORM\JoinColumn(nullable=false) 
    */ 
    private $author; 

    /** 
    * @ORM\Column(type="string", length=150, nullable=true) 
    * @Assert\NotBlank() 
    * @Assert\Length(
    *  min=5, 
    *  minMessage="Perex is too short!", 
    *  max=100, 
    *  maxMessage="Perex is too long!" 
    *) 
    */ 
    private $perex; 

    /** 
    * @ORM\Column(type="text", length=500, nullable=true) 
    * @Assert\NotBlank() 
    */ 
    private $text; 

    /** 
    * @var ArrayCollection 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\ProductPicture", mappedBy="product", cascade={"all"}, orphanRemoval=true) 
    */ 
    private $pictures; 

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

    /** 
    * @return ArrayCollection 
    */ 
    public function getPictures() 
    { 
     return $this->pictures; 
    } 

    /** 
    * @param ArrayCollection $pictures 
    */ 
    public function setPictures($pictures) 
    { 
     $this->pictures = $pictures; 
    } 

    public function getAttachPictures() 
    { 
     return null; 
    } 

    public function setAttachPictures(array $files=array()) 
    { 
     if (!$files) return []; 
     foreach ($files as $file) { 
      if (!$file) return []; 
      $this->attachPicture($file); 
     } 
     return []; 
    } 

    public function attachPicture(UploadedFile $file=null) 
    { 
     if (!$file) { 
      return; 
     } 
     $picture = new ProductPicture(); 
     $picture->setImageFile($file); 
     $this->addPicture($picture); 
    } 

    public function addPicture(ProductPicture $picture) 
    { 
     $picture->setProduct($this); 
     $this->pictures->add($picture); 
    } 

    public function removePicture(ProductPicture $picture) 
    { 
     $picture->setProduct(null); 
     $this->pictures->removeElement($picture); 
    } 
} 

ProductPicture:

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

    /** 
    * @var Product 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product", inversedBy="pictures") 
    */ 
    private $product; 

    /** 
    * 
    * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName", size="imageSize") 
    * @Assert\File(
    *  maxSize = "1024k", 
    *  mimeTypes = {"image/png", "image/jpeg", "image/jpg"}, 
    *  mimeTypesMessage = "Please upload a valid valid IMAGE" 
    *) 
    * 
    * @var File 
    */ 
    private $imageFile; 

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

    /** 
    * 
    * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image 
    * 
    * @return Product 
    */ 
    public function setImageFile(File $image = null) 
    { 
     $this->imageFile = $image; 
    } 

    public function getImageFile() 
    { 
     return $this->imageFile; 
    } 

    public function setProduct(Product $product) 
    { 
     $this->product = $product; 
    } 

    public function getProduct() 
    { 
     return $this->product; 
    } 

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

    /** 
    * @param mixed $id 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 
    } 

    /** 
    * @return array 
    */ 
    public function getImageName() 
    { 
     return $this->imageName; 
    } 

    /** 
    * @param array $imageName 
    */ 
    public function setImageName($imageName) 
    { 
     $this->imageName = $imageName; 
    } 

} 

形式:

class ProductType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('title') 
      ->add('perex') 
      ->add('text') 
      ->add('attachPictures', FileType::class, ['multiple'=>true, 'required'=>false]) 
     ; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults([ 
      'data_class' => Product::class, 
     ]); 
    } 
} 

回答

0

你应该建立文件部分作为一个实体

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

    /** 
    * @var Product 
    * @ORM\ManyToOne(targetEntity="path\to\your\entity\Product", inversedBy="pictures") 
    */ 
    private $product; 

    /** 
    * 
    * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName", size="imageSize") 
    * @Assert\File(
    *  maxSize = "1024k", 
    *  mimeTypes = {"image/png", "image/jpeg", "image/jpg"}, 
    *  mimeTypesMessage = "Please upload a valid valid IMAGE" 
    *) 
    * 
    * @var File 
    */ 
    private $imageFile; 

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

    /** 
    * 
    * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image 
    * 
    * @return Product 
    */ 
    public function setImageFile(File $image = null) 
    { 
     $this->imageFile = $image; 
    } 

    public function getImageFile() 
    { 
     return $this->imageFile; 
    } 

    public function setProduct(Product $product) 
    { 
     $this->product = $product; 
    } 

    public function getProduct() 
    { 
     return $this->product; 
    } 
} 

而在你的产品的实体...

/** 
    * @var ArrayCollection 
    * @ORM\OneToMany(targetEntity="path\to\your\entity\ProductPicture", mappedBy="product", cascade={"all"}, orphanRemoval=true) 
    */ 
    private $pictures; 

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

    /** 
    * @return ArrayCollection 
    */ 
    public function getPictures() 
    { 
     return $this->pictures; 
    } 

    /** 
    * @param ArrayCollection $pictures 
    */ 
    public function setPictures($pictures) 
    { 
     $this->pictures = $pictures; 
    } 

    public function getAttachPictures() 
    { 
     return null; 
    } 

    public function setAttachPictures(array $files=array()) 
    { 
     if (!$files) return []; 
     foreach ($files as $file) { 
      if (!$file) return []; 
      $this->attachPicture($file); 
     } 
     return []; 
    } 

    public function attachPicture(UploadedFile $file=null) 
    { 
     if (!$file) { 
      return; 
     } 
     $picture = new ProductPicture(); 
     $picture->setImageFile($file); 
     $this->addPicture($picture); 
    } 

    public function addPicture(ProductPicture $picture) 
    { 
     $picture->setProduct($this); 
     $this->pictures->add($picture); 
    } 

    public function removePicture(ProductPicture $picture) 
    { 
     $picture->setProduct(null); 
     $this->pictures->removeElement($picture); 
    } 

最后的形式使用...

->add('attachPictures', FileType::class, ['multiple'=>true, 'required'=>false]) 
+0

捕致命错误:传递给的appbundle \实体\产品:: attachPicture(参数1)必须的appbundle \实体\ UploadedFile的,Symfony的\组件实例的实例\ HttpFoundation \文件\ UploadedFile的给予 –

+0

其实我解决了这个eror,但我收到错误500 –

+0

检查在产品实体的顶部,你有没有'使用的appbundle \实体\ UploadedFile'使用,它必须是'使用的Symfony \分量\ HttpFoundation \文件\ UploadedFile' – Enumus

0

我不能帮你这个上传捆绑,但我可以告诉你它是如何工作的在我的情况下,更简单的方式(使用简单的请求,而不是一些花哨的束)

这里 https://gist.github.com/poznet/e3955a48fea01d8640dafbd966c53a83

你有

  • 鉴于形式
  • 控制器代码
  • 性状是在服务实体进口

这一特点这是一个有点乱(不attachemt尺寸/类型检查等),但它的工作原理。

如果您有任何疑问下跌免费问。

PS你的情况,如果你上传多个文件你阵列,而不是文件类:d