douyinglan2599 2012-01-19 09:47
浏览 25
已采纳

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

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?

  • 写回答

4条回答 默认 最新

  • drwxg62286 2012-01-19 13:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭