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

时间:2012-01-19 09:47:01

标签: php forms symfony symfony-forms

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

我想我某处有错,但我不知道在哪里。

这是我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">例如。

这是正确的方法吗?

4 个答案:

答案 0 :(得分:15)

感谢this answer,我找到了更好的解决方案。您可以使用Doctrine's orphan removal功能:

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);
    }
}

答案 1 :(得分:3)

现在,我这样做:

    [...]        
    $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会找到一个解决方案,但很难找到。

答案 2 :(得分:1)

symfony2中的表单集合非常简单,几乎可以为您完成所有工作。基本上,您只需添加collection type并设置allow_addallow_delete标记并添加一个小的JavaScript代码。看看cookbook example

答案 3 :(得分:0)

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

在控制器中:

$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);
    }

}

对我而言,它可以工作,它显示的代码更少,我不必使用orphanRemoval功能。