Symfony2表单集合:如何从集合中删除实体


Symfony2 form collection: How to remove entity from a collection?

我试图从集合中删除实体,但它不起作用。

我想我在哪里搞错了,但我不知道在哪里。

这里是我的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">

这是正确的方式吗?

多亏了这个答案,我找到了更好的解决方案。您可以使用Doctrine的孤儿移除功能:

class Gallery
{
    //...    
    /**
     * @ORM'OneToMany(targetEntity="Photo", mappedBy="gallery", cascade={"persist", "remove"}, orphanRemoval=true)
     */
    private $photos;
    //...
    public function removePhotos($photo)
    {
        $this->photos->remove($photo);
        $photo->setGallery(null);
    }
}

现在,我做:

    [...]        
    $editForm   = $this->createForm(new PersonType(), $entity);
    $deleteForm = $this->createDeleteForm($id);
    $previousCollections = array(
        'addresses' => $entity->getAddresses(),
    );        
    $request = $this->getRequest();
    $editForm->bindRequest($request);
    if ($editForm->isValid()) {
        $entity = $editForm->getData();
        $this->deleteCollections($em, $previousCollections, $entity);
        $em->persist($entity);
        foreach($entity->getAddresses() as $address)
        {               
            $em->persist($address);
        }
        $em->flush();                                 
        return $this->redirect($this->generateUrl('person_show', array('id' => $id)));
    }
    [...]
}
private function deleteCollections($em, $init, $final)
{
    if (empty($init)) {
        return;
    }
    if (!$final->getAddresses() instanceof 'Doctrine'ORM'PersistentCollection) {
        foreach ($init['addresses'] as $addr) {
            $em->remove($addr);
        }
    }
}

我希望有一天能找到解决方案https://github.com/symfony/symfony/issues/1540,但发现速度很慢。

symfony2中的Form集合非常简单,它几乎为您完成了所有的工作。基本上,您只需要添加一个collection type并设置allow_addallow_delete标志,然后添加一个小的JavaScript代码。看看食谱示例

我正在使用这个解决方案。。。

在控制器中:

$em = $this->getDoctrine()->getManager();
$enity->removeElements($em);
//Add all your elements again in order to update entity collection.
$entity->addElement($element) ...
....
$em->persist($entity);
$em->flush();

在实体中:

public function removeElements($em)
{
    $elements = $this->elements;
    foreach ($elements as $element) {
        $this->elements->removeElement($element);
        $em->remove($element);
        $em->persist($this);
    }
}

对我来说,它很有效,显示的代码更少,而且我不必使用orphanRemove功能。