即时寻找多个上传。我也有工作上传,但仍然只有一个文件。我找不到解决我的问题的答案。我也尝试了很多教程,但没有为我工作。在他们的文档中我找不到答案。我该怎么办?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,
]);
}
}
捕致命错误:传递给的appbundle \实体\产品:: attachPicture(参数1)必须的appbundle \实体\ UploadedFile的,Symfony的\组件实例的实例\ HttpFoundation \文件\ UploadedFile的给予 –
其实我解决了这个eror,但我收到错误500 –
检查在产品实体的顶部,你有没有'使用的appbundle \实体\ UploadedFile'使用,它必须是'使用的Symfony \分量\ HttpFoundation \文件\ UploadedFile' – Enumus