Symfony2 form collection: How to remove entity from a collection?(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.
这篇关于Symfony2 表单集合:如何从集合中删除实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Symfony2 表单集合:如何从集合中删除实体?
				
        
 
            
        - 如何在 Symfony2 中正确使用 webSockets 2021-01-01
 - Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
 - openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
 - 覆盖 Magento 社区模块控制器的问题 2022-01-01
 - 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
 - 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
 - PHP foreach() 与数组中的数组? 2022-01-01
 - PHP - if 语句中的倒序 2021-01-01
 - 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
 - Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
 
