Symfony2表单集合:如何从集合中删除实体? [英] Symfony2 form collection: How to remove entity from a collection?
问题描述
我尝试从集合中删除实体,但它不起作用。
我认为我在某处有个错误,但我不知道在哪里。
我的 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)));
$ b $ return $ this-> render('AppPersonBundle:Person:edit.html.twig',array(
'entity'=> $ entity,
'edit_form'=> $ editForm-> createView(),
'delete_form'=> $ deleteForm-> createView(),
$ b $;
请注意,要删除我的实体,请从html中删除div。
我的意思是,例如,我删除了< div id =myapp_personbundle_persontype_address_4>
。
它是正确的方式?
现在,我做:
$ deleteForm = $ this-> createDeleteForm($ id);$ previousCollections = array(
'addresses'=> $ entity-> getAddresses(),
);
$ request = $ this-> getRequest();
$ editForm-> bindReq ($请求);
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)));
}
[...]
}
私有函数deleteCollections($ em,$ init,$ final)
{
if (空($ init)){
return;
if(!$ final-> getAddresses()instanceof \Doctrine\ORM\PersistentCollection){
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 \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.
这篇关于Symfony2表单集合:如何从集合中删除实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!