Symfony2中的getEntityManager()和getDoctrine()

Nig*_*l85 8 symfony doctrine-orm

这两个陈述之间有什么区别:

$this->getDoctrine()->getEntityManager()->getRepository();

$this->getDoctrine()->getRepository();
Run Code Online (Sandbox Code Playgroud)

差异是否与我错过的任何OOP概念有关?

Eln*_*mov 8

一般来说,没有区别,因为

$this->getDoctrine()->getRepository();
Run Code Online (Sandbox Code Playgroud)

只是一个帮手

$this->getDoctrine()->getEntityManager()->getRepository();
Run Code Online (Sandbox Code Playgroud)

您可以拥有多个实体管理器,然后从一个实体库中获取存储库会略有不同:

$this->getDoctrine()->getRepository($entityName, $enityManagerName);
$this->getDoctrine()->getEntityManager($entityManagerName)->getRepository($entityName);
Run Code Online (Sandbox Code Playgroud)

但同样,你得到的结果没有区别.

在所有其他条件相同的情况下,我会选择最短的.