<tfoot id='3lrmr'></tfoot>

      <small id='3lrmr'></small><noframes id='3lrmr'>

      <i id='3lrmr'><tr id='3lrmr'><dt id='3lrmr'><q id='3lrmr'><span id='3lrmr'><b id='3lrmr'><form id='3lrmr'><ins id='3lrmr'></ins><ul id='3lrmr'></ul><sub id='3lrmr'></sub></form><legend id='3lrmr'></legend><bdo id='3lrmr'><pre id='3lrmr'><center id='3lrmr'></center></pre></bdo></b><th id='3lrmr'></th></span></q></dt></tr></i><div id='3lrmr'><tfoot id='3lrmr'></tfoot><dl id='3lrmr'><fieldset id='3lrmr'></fieldset></dl></div>
        <legend id='3lrmr'><style id='3lrmr'><dir id='3lrmr'><q id='3lrmr'></q></dir></style></legend>
          <bdo id='3lrmr'></bdo><ul id='3lrmr'></ul>

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

        时间:2023-08-18

            <tbody id='1jsQo'></tbody>
          <i id='1jsQo'><tr id='1jsQo'><dt id='1jsQo'><q id='1jsQo'><span id='1jsQo'><b id='1jsQo'><form id='1jsQo'><ins id='1jsQo'></ins><ul id='1jsQo'></ul><sub id='1jsQo'></sub></form><legend id='1jsQo'></legend><bdo id='1jsQo'><pre id='1jsQo'><center id='1jsQo'></center></pre></bdo></b><th id='1jsQo'></th></span></q></dt></tr></i><div id='1jsQo'><tfoot id='1jsQo'></tfoot><dl id='1jsQo'><fieldset id='1jsQo'></fieldset></dl></div>

          <small id='1jsQo'></small><noframes id='1jsQo'>

          <tfoot id='1jsQo'></tfoot>
          <legend id='1jsQo'><style id='1jsQo'><dir id='1jsQo'><q id='1jsQo'></q></dir></style></legend>

                <bdo id='1jsQo'></bdo><ul id='1jsQo'></ul>

                • 本文介绍了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 表单集合:如何从集合中删除实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <tbody id='eRbLS'></tbody>
                • <i id='eRbLS'><tr id='eRbLS'><dt id='eRbLS'><q id='eRbLS'><span id='eRbLS'><b id='eRbLS'><form id='eRbLS'><ins id='eRbLS'></ins><ul id='eRbLS'></ul><sub id='eRbLS'></sub></form><legend id='eRbLS'></legend><bdo id='eRbLS'><pre id='eRbLS'><center id='eRbLS'></center></pre></bdo></b><th id='eRbLS'></th></span></q></dt></tr></i><div id='eRbLS'><tfoot id='eRbLS'></tfoot><dl id='eRbLS'><fieldset id='eRbLS'></fieldset></dl></div>

                  <tfoot id='eRbLS'></tfoot>

                    1. <small id='eRbLS'></small><noframes id='eRbLS'>

                      • <bdo id='eRbLS'></bdo><ul id='eRbLS'></ul>
                        <legend id='eRbLS'><style id='eRbLS'><dir id='eRbLS'><q id='eRbLS'></q></dir></style></legend>