且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Doctrine entitymanager clear 没有完全清除

更新时间:2023-09-01 08:34:58

我想说,从技术上讲,这不是一个错误,因为 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() 方法只是一种detach() 指定类型的所有实体或实体的方法.它可以被认为是一个Multi-Detach",它的目的并不超出分离.

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() 分离所有实体时,Doctrine 使用最有效的方法分离实体.在这个过程中,身份映射数组被设置为一个空的array().这将使我相信您所指的内容看起来已清除.

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.

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

如果我们假设为 RezaMyBundleEntityListItem 的实体检索了数据.那么在下面的例子中我们可以看到工作单元至少有 1 个 RezaMyBundleEntityListItem 对象.

If we assume that data was retrieved for an entity of RezaMyBundleEntityListItem. Then in the following example we can see that the unit of work has at least 1 RezaMyBundleEntityListItem object.

$identity = $entityManager->getUnitOfWork()->getIdentityMap();
$count = count($identity['RezaMyBundleEntityListItem']);
// $count would be > 0;

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

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.

$entityManager->clear('RezaMyBundleEntityListItem');
$identity = $entityManager->getUnitOfWork()->getIdentityMap(); 
$count = count($identity['RezaMyBundleEntityListItem']);
//$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 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).