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 表单集合:如何从集合中删除实体?
基础教程推荐
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01