Symfony:分离的实体无法在带有phpunit的测试套件中删除 [英] Symfony : Detached entity cannot be removed in test suite w/ phpunit

查看:83
本文介绍了Symfony:分离的实体无法在带有phpunit的测试套件中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上正在对api进行一些功能测试,并且遇到了一个我不太了解的问题.

I'm actually doing some functionnal testing on my api and I'm facing a problem which I don't really understand.

我想测试与远程Web托管服务器交互的API.目标是管理VirtualHosts,DNS区域,数据库等. 我有一个测试远程服务器,为避免冲突,我在测试后删除了创建的内容(在TearDown()函数中)并创建了基础(在setUp()函数中).在setUp()中,我还在安装程序中加载了夹具.

I want to test an API which interact with a remote web hosting server. The goal is to manage VirtualHosts, DNS zones, database etc... I have a test remote server and to avoid conflicts, I remove the created stuff after the test (in the TearDown() function) and create the base (in the setUp() function). In the setUp() I also load fixtures in the setup.

在添加了我的主要实体的子代的测试之后,我想清理远程服务器:

After a test which add a child of my main entity, I want to clean the remote server :

$service = $this->fixtures->getReference('service-web');
$this->container->get('webmanager')->deleteHosting($service, true);

deleteHosting()函数将删除所有远程内容(真正的参数是"force"参数,以确保该函数不会在发生错误后停止.

The deleteHosting() function deletes all remote stuff (the true parameter is the "force" parameter, which ensure that the function doesn't stop after an error.

$service变量包含我的主要服务实体.在这个实体中,我还与addonDomain的实体存在一对多关系. 我的功能测试创建了一个addonDomain. 测试还可以,但是当我尝试删除我的服务时,附加的实体使Doctrine大喊大叫:Doctrine\ORM\ORMInvalidArgumentException: Detached entity AppBundle\Entity\Service\Web\AddonDomain@0000000054814da900000000073c524a cannot be removed.

The $service variable contains my main service entity. I also have in this entity a One-To-Many relationship with addonDomain's entity. My functionnal test creates an addonDomain. The test is OK, but when I try to delete my service, the attached entity makes Doctrine yell as hell : Doctrine\ORM\ORMInvalidArgumentException: Detached entity AppBundle\Entity\Service\Web\AddonDomain@0000000054814da900000000073c524a cannot be removed.

我尝试了很多事情,但都无济于事(即:使用学说管理器来检索实体,而不是使用固定装置.

I tried a lot of things but none of them works (ie : using the doctrine manager to retrieve the entity instead of using the fixture.

非常感谢您的帮助, 最好的问候.

Thanks a lot for your help, best regards.

已解决! 实际上,我使用的是测试类的容器和实体管理器,而不是客户本人的实体管理器.那是问题所在...

SOLVED ! In fact, I'm using the container and the entity manager of the test class and not of the client himself. That was the problem...

推荐答案

尽管这是一个老问题,但最近在使用Symfony 4.4和

Although this is an old question, I had the same problem recently while working with Symfony 4.4 and Detached entity error in Doctrine helped me solve the problem. Basically, as @Can Vural suggested, calling merge before remove is solving the problem, but the missing bit is that you have to assign the entity again so:

$entity = $doctrineManager->merge($detachedEntity);  
$doctrineManager->remove($entity);
$doctrineManager->flush();

这解决了我的问题.

请记住,merge已过时,将在《教义3》中删除: https://github.com/doctrine/orm/blob/master/UPGRADE.md#bc-break-removed-entitymanagermergemerge-and-entitymanagerdetach-methods

Please keep in mind that merge is deprecated and will be removed in Doctrine 3: https://github.com/doctrine/orm/blob/master/UPGRADE.md#bc-break-removed-entitymanagermerge-and-entitymanagerdetach-methods

这篇关于Symfony:分离的实体无法在带有phpunit的测试套件中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆