Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I try to remove entities from a collection but it doesn't work.

I think I have a mistake somewhere, but I don't know where.

Here the code from my 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(),

    );

Note that to remove my entity I remove the div from the html.

I mean I remove <div id="myapp_personbundle_persontype_address_4"> for example.

Is it the right way?

like image 970
Sam Avatar asked Jan 19 '12 09:01

Sam


4 Answers

Thanks to this answer, I found a better solution. You can use Doctrine's orphan removal feature:

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);
    }
}
like image 116
Stephan Vierkant Avatar answered Nov 04 '22 00:11

Stephan Vierkant


For now, i do :

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

And I hope a solution will be found one day with https://github.com/symfony/symfony/issues/1540, but it slow to be found.

like image 31
webda2l Avatar answered Nov 03 '22 22:11

webda2l


Form collection in symfony2 is quite straightforward, it does almost all the work for you. Basically you just need add a collection type and set allow_add, allow_delete flags and add a small JavaScript code. Have a look at the cookbook example

like image 30
Amadu Bah Avatar answered Nov 03 '22 23:11

Amadu Bah


I'm using this solution...

In the controler:

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

In the entity:

public function removeElements($em)
{
    $elements = $this->elements;

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

}

For me it works and it shows less code and I don't have to use the orphanRemoval feature.

like image 28
Victor Salvans Montesó Avatar answered Nov 04 '22 00:11

Victor Salvans Montesó