我试图使用窗体将图像URL存储到数据库。与symfony ManyToOne的关系在数据库中存储图像URL
我有2个实体:
产品& ProductImages。
产品与ProductImages具有OneToMany关系,并且 ProductImages与产品具有ManyToOne关系。
产品:
/**
* @ORM\OneToMany(targetEntity="ProductImages", mappedBy="product")
*/
private $images;
/**
* @return mixed
*/
public function getImages()
{
return $this->images;
}
/**
* @param mixed $images
* @return $this
*/
public function setImages($images)
{
$this->images = $images;
return $this;
}
ProductImages:
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="images")
* @ORM\JoinColumn(nullable=false)
*/
private $product;
/**
* @return mixed
*/
public function getProduct()
{
return $this->product;
}
/**
* @param mixed $product
*/
public function setProduct(Product $product)
{
$this->product = $product;
}
我做了一个形式,具有文件上传和此控制器:
$product = new Product();
$form = $this->createForm(ProductFormType::class, $product);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$file = $product->getImages();
$fileName = $form->get('slug')->getData().'.'.$file->guessExtension();
$file->move(
$this->getParameter('product_image_directory'),
$fileName
);
$product->setImages($fileName);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
$this->addFlash('success', 'Product aangemaakt!');
return $this->redirectToRoute('admin_product_list');
}
我得到的错误是:
Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, string given, called in C:\git\symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 605
我已经尝试将ProductImages保存为new ProductImages();
并填写所有字段,但这也不起作用。
它必须是产品图片的数组,你必须改为传递对象数组 – Eimsas