我的应用程序,列表和联系人中有2个实体。我和许多人都有双向关系。
我想创建一个包含多个联系人的列表,一个联系人属于一个或多个列表。
问题是:我添加的联系人与列表无关。
以下是代码:
list.php的
//...
/**
* @ORM\ManyToMany(targetEntity="MyBundle\Entiy\Contact,inversedBy="lists")
* @ORM\JoinTable(name="list_contact")
*/
private $contacts;
//...
contact.php
//...
/**
* @ORM\ManyToMany(targetEntity="MyBundle\Entity\List",mappedBy="contacts")
*/
private $lists;
//...
ListController.php
public function editAction(Request $request, List $list)
{
// edit form
$em = $this->getDoctrine()->getManager();
$lists = $em->getRepository('MyBundle:List')->findAll();
$deleteForm = $this->createDeleteForm($list);
$editForm = $this->createForm('MyBundle\Form\ListType', $list);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('list_custom', array('id' => $list->getId()));
}
// new contact
$contact = new Contact();
$contact->addList($list);
$form = $this->createForm('MyBundle\Form\ContactType', $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($contact);
$em->flush();
return $this->redirectToRoute('list_edit', array('id' => $contact->getId()));
}
//return
return $this->render('@MyBundle/List/edit_list.html.twig', array(
'list' => $list,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
'lists' => $lists,
'contact' => $contact,
'form' => $form->createView(),
));
}