我尝试从集合中删除实体,但它不起作用。Symfony2表单集合:如何从集合中删除实体?
我想我在某个地方有一个错误,但我不知道在哪里。
下面的代码从我updateAction
:
$em = $this->getDoctrine()->getEntityManager();
$entity = new Person();
if (!$entity) {
throw $this->createNotFoundException('Unable to find Person entity.');
}
$editForm = $this->createForm(new PersonType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$entity = $editForm->getData();
$em->persist($entity);
foreach($entity->getAddresses() as $address)
{
$em->persist($address);
}
$em->flush();
return $this->redirect($this->generateUrl('person_show', array('id' => $id)));
}
return $this->render('AppPersonBundle:Person:edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
请注意,除去我的实体我从HTML删除DIV。例如,我删除了<div id="myapp_personbundle_persontype_address_4">
。
这是正确的方式吗?
非常感谢你这么多帮助我。我还有一个问题是可以做一个比较2 ArrayCollection比较? – Sam
不是原生的,但在API的帮助下(http://www.doctrine-project.org/api/common/2.1/doctrine/common/collections/arraycollection.html),你应该成功地 – webda2l
好吧......谢谢所有山姆 – Sam