教义实体管理员明确并不完全清楚 [英] Doctrine entitymanager clear doesn't fully clear

查看:138
本文介绍了教义实体管理员明确并不完全清楚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

$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;
    }
}

你会认为,在我将类名传给清楚的是,你不应该在工作单元中看到这些对象,但是通过查看源代码,我注意到当你将参数传递给 clear()函数时, strong>仅分离该类型的实体。另一方面,如果我没有将任何参数传递给 clear(),它会分离并确实清除,因此上述代码不会导致第138行退出。所以这意味着它不仅分离所有实体,而且还清除了工作单位。

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.

任何人都有这样的想法?我应该用教条提交错误吗?

Anyone has any thoughts on this? Should I file a bug with doctrine?

推荐答案

我会说在技术上它不是一个错误,因为 clear()按文档中所述进行操作,请参阅Doctrine2 API 文档源代码当前版本)。

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).

clear()方法只是一种指定类型的所有实体或实体。
它可以被认为是多分离,它的目的不是延伸过去的分离。

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.

使用清除所有实体时清除()教义使用最有效的方法分离实体。在此过程中,身份映射数组被设置为一个空的数组()
这将给出我相信你所说的被清除的出现。

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.

$entityManager->clear();
$identity = $entityManager->getUnitOfWork()->getIdentityMap(); 
//This will return a an empty array() to $identity
//therefore $identity['Reza\MyBundle\Entity\ListItem'] would be undefined

如果我们假设为Reza\MyBundle\Entity\ListItem的实体检索数据。
然后在下面的例子中,我们可以看到工作单元至少有1个'Reza\MyBundle\Entity\ListItem'对象。 p>

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.

$identity = $entityManager->getUnitOfWork()->getIdentityMap();
$count = count($identity['Reza\MyBundle\Entity\ListItem']);
//$count would be > 0;

但是当您使用 clear($ entityName)并按实体类型清除,清除/分离的实体将从工作单元中删除​​,只有数组键 [$ entityName] ,而不是任何对象。

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.

$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.

我认为功能请求是有序的,以使其工作更加一致。
当调用 clear($ entityName) Doctrine应该 unset()剩余的键,从而使其不定义清除)。这将允许我们更容易地编写代码,无论我们是否使用 clear() clear($ entityName)

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).

这篇关于教义实体管理员明确并不完全清楚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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