I'm trying to store the image URL to the database using a form.
I've got 2 entities:
Product & ProductImages.
Product has OneToMany relation to ProductImages and ProductImages has ManyToOne relation to Product.
Product:
/**
* @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;
}
I made a form, with a file upload and this controller:
$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');
}
The error I'm getting is :
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
I already tried to save the ProductImages as new ProductImages();
and filling in all fields, but that does not work either.