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 detaches entities 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()函数时它只会分离实体那种类型。另一方面,如果我没有传递任何参数clear()它分离并确实清楚,所以上面的代码没有命中第138行,退出。这意味着它不仅可以分离所有实体,还可以清除工作单元。
Anyone has any thoughts on this? Should I file a bug with doctrine?
有没有人对此有任何想法?我应该提交一个带有学说的错误吗?
1 个解决方案
#1
29
I would say that technically it is not a bug as clear()
works as described in the documentation, see Doctrine2 API, documentation and source code (current version).
我会说技术上它不是一个bug,因为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", it's purpose does not extend past detaching.
clear()方法只是一种分离()指定类型的所有实体或实体的方法。它可以被认为是“多分离”,它的目的不会延伸过去分离。
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 appears of what I believe you are referring to as cleared.
使用clear()Doctrine分离所有实体时,可以使用最有效的方法分离实体。在此过程中,Identity Map 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 follow 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 remove 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 the a feature request is 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应该取消设置()剩余的键,从而使其未定义(清除)。这将允许我们更容易编写无论我们使用clear()还是clear($ entityName)都能工作的代码。
#1
29
I would say that technically it is not a bug as clear()
works as described in the documentation, see Doctrine2 API, documentation and source code (current version).
我会说技术上它不是一个bug,因为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", it's purpose does not extend past detaching.
clear()方法只是一种分离()指定类型的所有实体或实体的方法。它可以被认为是“多分离”,它的目的不会延伸过去分离。
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 appears of what I believe you are referring to as cleared.
使用clear()Doctrine分离所有实体时,可以使用最有效的方法分离实体。在此过程中,Identity Map 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 follow 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 remove 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 the a feature request is 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应该取消设置()剩余的键,从而使其未定义(清除)。这将允许我们更容易编写无论我们使用clear()还是clear($ entityName)都能工作的代码。