学说实体管理者明确并不完全清楚

Rez*_*a S 14 php doctrine symfony doctrine-orm

我有这段代码:

$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;
    }
}
Run Code Online (Sandbox Code Playgroud)

您会认为在我将类名传递给clear之后,您不应再在工作单元中看到这些对象,但是通过查看源代码我注意到当您将参数传递给clear()函数时它只会分离该类型的实体.另一方面,如果我没有传递任何参数,clear()它就会分离并确实清楚,所以上面的代码没有命中第138行,退出.这意味着它不仅可以分离所有实体,还可以清除工作单元.

有没有人对此有任何想法?我应该提交一个带有学说的错误吗?

hco*_*oat 34

我会说技术上它不是clear()文档中描述的工作的bug ,请参阅Doctrine2 API,文档源代码(当前版本).

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

使用clear()Doctrine分离所有实体时,可以使用最有效的方法分离实体.在此过程中,Identity Map Array设置为空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
Run Code Online (Sandbox Code Playgroud)

如果我们假设为'Reza\MyBundle\Entity\ListItem'的实体检索了数据.然后在下面的示例中,我们可以看到Reza\MyBundle\Entity\ListItem至少有1个'Reza\MyBundle\Entity\ListItem'对象.

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

但是,当您Reza\MyBundle\Entity\ListItem按实体类型使用和清除时,清除/分离的实体将从中删除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.
Run Code Online (Sandbox Code Playgroud)

此功能是文档指定的全部功能.

我确实认为功能请求是有序的,以使其更加一致.当调用clear($entityName)Doctrine时unset(),剩余的键应该使其未定义(清除).这将使我们能够更轻松地编写无论我们使用clear()或使用的代码clear($entityName).

  • @hcoat请不要将(3)链接到GitHub上主分支中的特定行,因为它很可能会改变(它已经完成).选择标记的版本. (3认同)
  • 一切都那么简单.它仍然只是地图中类的名称,但实体消失了.做得好! (2认同)