我試圖使用窗體將圖像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