本文介绍了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 表单集合:如何从集合中删除实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20
php开发问题
168
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23
php开发问题
287
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23
php开发问题
11
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23
php开发问题
4
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23
php开发问题
17

大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)