product
我在和表之间有多对多关系,product_details
这导致第三个表称为product_has_product_details
. 我做了这个代码:
ProductBundle\Entity\ProductDetail.php
/**
* @ORM\ManyToMany(targetEntity="CategoryBundle\Entity\Category", mappedBy="pd_category")
*/
protected $category;
public function __construct() {
$this->category = new \Doctrine\Common\Collections\ArrayCollection();
}
public function setCategory($category) {
$this->category[] = $category;
}
CategoryBundle\Entity\Category.php
/**
* @ORM\ManyToMany(targetEntity="ProductBundle\Entity\ProductDetail", inversedBy="category")
* @ORM\JoinTable(name="product_detail_has_category")
*/
protected $pd_category;
public function __construct() {
$this->pd_category = new \Doctrine\Common\Collections\ArrayCollection();
}
在我的控制器ProductDetailController.php
中有这个代码:
/**
* Handle category creation
*
* @Route("/pdetail/create", name="pdetail_create")
* @Method("POST")
* @Template("ProductBundle:ProductDetail:new.html.twig")
*/
public function createAction(Request $request) {
$entity = new ProductDetail();
$form = $this->createForm(new ProductDetailType(), $entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity->setValuesText(serialize($form->get('values_text')->getData()));
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('pdetail_list'));
}
return $this->render('ProductBundle:ProductDetail:new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
当我发送表单时,ProductDetail 的值被保存,但关系的值没有,我找不到问题出在哪里。我也收到此错误:
控制器必须返回响应 (Array(product_details => Array(0 => Object(ProductBundle\Entity\ProductDetail), 1 => Object(ProductBundle\Entity\ProductDetail), 2 => Object(ProductBundle\Entity\ProductDetail)) )给定)。
有什么可以帮助我的吗?