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

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

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

这里是我的 updateAction 中的代码:

 $em = $this->getDoctrine()->getEntityManager();$entity = new Person();如果(!$实体){throw $this->createNotFoundException('无法找到 Person 实体.');}$editForm = $this->createForm(new PersonType(), $entity);$deleteForm = $this->createDeleteForm($id);$request = $this->getRequest();$editForm->bindRequest($request);如果 ($editForm->isValid()) {$entity = $editForm->getData();$em->persist($entity);foreach($entity->getAddresses() 作为 $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('实体' =>$实体,'edit_form' =>$editForm->createView(),'delete_form' =>$deleteForm->createView(),);

请注意,要删除我的实体,我会从 html 中删除 div.

我的意思是我删除了

例如.

这样做对吗?

解决方案

现在,我这样做:

<代码> [...]$editForm = $this->createForm(new PersonType(), $entity);$deleteForm = $this->createDeleteForm($id);$previousCollections = 数组('地址' =>$entity->getAddresses(),);$request = $this->getRequest();$editForm->bindRequest($request);如果 ($editForm->isValid()) {$entity = $editForm->getData();$this->deleteCollections($em, $previousCollections, $entity);$em->persist($entity);foreach($entity->getAddresses() 作为 $address){$em->persist($address);}$em->flush();return $this->redirect($this->generateUrl('person_show', array('id' => $id)));}[...]}私有函数 deleteCollections($em, $init, $final){如果(空($init)){返回;}if (!$final->getAddresses() instanceof DoctrineORMPersistentCollection) {foreach ($init['addresses'] as $addr) {$em->remove($addr);}}}

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

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?

解决方案

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 DoctrineORMPersistentCollection) {
        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.

相关文章