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