Symfony2表单集合:如何从集合中删除实体?
我尝试从集合中删除实体,但它不起作用。
我想我在某个地方犯了错误,但我不知道在哪里。
这是我的 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(),
);
请注意,要删除我的实体,我从 html 中删除了 div。
我的意思是我删除了
例如。这是正确的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢这个答案,我找到了更好的解决方案。您可以使用 Doctrine 的孤儿移除功能:
Thanks to this answer, I found a better solution. You can use Doctrine's orphan removal feature:
现在,我这样做:
我希望有一天能够通过 https://github 找到解决方案。 com/symfony/symfony/issues/1540,但查找速度很慢。
For now, i do :
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 中的表单收集非常简单,它几乎可以为您完成所有工作。基本上,您只需要添加一个
集合类型
并设置allow_add
、allow_delete
标志并添加一个小的JavaScript代码。查看食谱示例Form collection in symfony2 is quite straightforward, it does almost all the work for you. Basically you just need add a
collection type
and setallow_add
,allow_delete
flags and add a small JavaScript code. Have a look at the cookbook example我正在使用这个解决方案...
在控制器中:
在实体中:
对我来说它有效并且显示的代码较少,而且我不必使用 orphanRemoval 功能。
I'm using this solution...
In the controler:
In the entity:
For me it works and it shows less code and I don't have to use the orphanRemoval feature.