php Doctrine entitymanager clear 没有完全清除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19061870/
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
Doctrine entitymanager clear doesn't fully clear
提问by Reza S
I have this piece of code:
我有这段代码:
$entityManager->clear('Reza\MyBundle\Entity\ListItem');
$identity = $entityManager->getUnitOfWork()->getIdentityMap();
foreach ($identity as $class => $objectlist) {
if ($class == 'Reza\MyBundle\Entity\ListItem') {
print "didn't fully clear, exiting..\n ";
exit;
}
}
You would think that after I pass in the classname to clear, you should not see those objects in the unit of work anymore, but by looking at the source I noticed that when you pass an argument to the clear()
function it only detachesentities of that type. On the other hand, if I don't pass any arguments to clear()
it detaches and does in fact clear, so the above code does not hit line 138, exit. So that means it not only detaches all entities, but also clears the unit of work.
您会认为在我传入类名以清除之后,您不应再在工作单元中看到这些对象,但是通过查看源代码,我注意到当您将参数传递给clear()
函数时,它只会分离该类型的实体. 另一方面,如果我不向clear()
它传递任何参数,它会分离并且实际上清除,所以上面的代码不会碰到第 138 行,退出。所以这意味着它不仅分离了所有实体,而且清除了工作单元。
Anyone has any thoughts on this? Should I file a bug with doctrine?
有人对此有任何想法吗?我应该用学说提交错误吗?
回答by hcoat
I would say that technically it is not a bug as clear()
works as described in the documentation, see Doctrine2 API, documentationand source code(current version).
我会说,从技术上讲,它不是clear()
文档中描述的那样工作的错误,请参阅 Doctrine2 API、文档和源代码(当前版本)。
The clear()
method is just a way to detach()
all entities or entities of a specified type.
It can be thought as a "Multi-Detach", its purpose does not extend past detaching.
该clear()
方法只是对detach()
所有实体或指定类型的实体的一种方式。它可以被认为是一个“多重分离”,它的目的并不超出分离。
When detaching all entities using clear()
Doctrine detaches the entities using the most efficient method possible. In the process the Identity Map Array is set to an empty array()
.
This will give the appearance of what I believe you are referring to as cleared.
当使用clear()
Doctrine 分离所有实体时,使用最有效的方法分离实体。在此过程中,身份映射数组设置为空的array()
。这将使我相信您所指的内容看起来已清除。
$entityManager->clear();
$identity = $entityManager->getUnitOfWork()->getIdentityMap();
//This will return a an empty array() to $identity
//therefore $identity['Reza\MyBundle\Entity\ListItem'] would be undefined
If we assume that data was retrieved for an entity of Reza\MyBundle\Entity\ListItem
.
Then in the following example we can see that the unit of work has at least 1 Reza\MyBundle\Entity\ListItem
object.
如果我们假设为 的实体检索了数据Reza\MyBundle\Entity\ListItem
。那么在下面的例子中我们可以看到工作单元至少有 1 个Reza\MyBundle\Entity\ListItem
对象。
$identity = $entityManager->getUnitOfWork()->getIdentityMap();
$count = count($identity['Reza\MyBundle\Entity\ListItem']);
// $count would be > 0;
However, when you use clear($entityName)
and clear by entity type, the cleared/detached entities are removed from the unit of work, it is only the array key [$entityName]
that remains, not any of the objects.
但是,当您clear($entityName)
按实体类型使用和清除时,已清除/分离的实体将从工作单元中删除,仅[$entityName]
保留数组键,而不是任何对象。
$entityManager->clear('Reza\MyBundle\Entity\ListItem');
$identity = $entityManager->getUnitOfWork()->getIdentityMap();
$count = count($identity['Reza\MyBundle\Entity\ListItem']);
//$count would be == 0. All Objects cleared/detached.
This functionality is all that is specified by the documentation.
此功能是文档指定的全部内容。
I do think a feature requestis in order, to make it work more consistently.
When invoking clear($entityName)
Doctrine should unset()
the remaining key thereby making it undefined (cleared). This would allow us to more easily write code that would work whether we used clear()
or clear($entityName)
.
我确实认为功能请求是为了使其更一致地工作。当调用clear($entityName)
Doctrine 时unset()
,剩余的键应该是未定义的(清除)。这将使我们能够更轻松地编写无论使用clear()
还是clear($entityName)
.