php Symfony2 表单集合:如何从集合中删除实体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8923919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2 form collection: How to remove entity from a collection?
提问by Sam
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
:
这是我的代码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.
请注意,要删除我的实体,我从 html 中删除了 div。
I mean I remove <div id="myapp_personbundle_persontype_address_4">
for example.
我的意思是我删除<div id="myapp_personbundle_persontype_address_4">
例如。
Is it the right way?
这是正确的方法吗?
采纳答案by webda2l
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.
我希望有一天能通过https://github.com/symfony/symfony/issues/1540找到解决方案,但找到的速度很慢。
回答by Stephan Vierkant
Thanks to this answer, I found a better solution. You can use Doctrine's orphan removalfeature:
感谢这个答案,我找到了一个更好的解决方案。您可以使用Doctrine 的孤儿移除功能:
class Gallery
{
//...
/**
* @ORM\OneToMany(targetEntity="Photo", mappedBy="gallery", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $photos;
//...
public function removePhotos($photo)
{
$this->photos->remove($photo);
$photo->setGallery(null);
}
}
回答by Amadu Bah
Form collection in symfony2 is quite straightforward, it does almost all the work for you. Basically you just need add a collection type
and set allow_add
, allow_delete
flags and add a small JavaScript code. Have a look at the cookbook example
symfony2 中的表单集合非常简单,它几乎可以为您完成所有工作。基本上你只需要添加一个collection type
和 set allow_add
,allow_delete
flags 并添加一个小的 JavaScript 代码。看一下食谱示例
回答by Victor Salvans Montesó
I'm using this solution...
我正在使用此解决方案...
In the controler:
在控制器中:
$em = $this->getDoctrine()->getManager();
$enity->removeElements($em);
//Add all your elements again in order to update entity collection.
$entity->addElement($element) ...
....
$em->persist($entity);
$em->flush();
In the entity:
在实体中:
public function removeElements($em)
{
$elements = $this->elements;
foreach ($elements as $element) {
$this->elements->removeElement($element);
$em->remove($element);
$em->persist($this);
}
}
For me it works and it shows less code and I don't have to use the orphanRemoval feature.
对我来说它有效,它显示的代码更少,我不必使用 orphanRemoval 功能。