使用Doctrine从数据库中检索用户

时间:2016-01-09 17:27:33

标签: orm doctrine symfony

这些命令有什么区别?

$this->getDoctrine()->getRepository('AppBundle:User')->find($id);

$this->getDoctrine()->getManager()->getRepository('AppBundle:User')->find($id);

我可以省略->getManager()并且我有相同的"正确"结果

1 个答案:

答案 0 :(得分:3)

没有区别。两种方式都使用由服务容器注入的一个存储库工厂实例。

如果您使用多个实体管理器(例如,多个连接)以简化生活,您可以使用$this->getDoctrine()->getRepository('AppBundle:User')->find($id)来简化。它将确定正确的实体经理本身。

因此,如果您有多个实体经理,我建议您始终使用不带->getManager()的方法来避免混淆其他开发人员。

相关问题