【问题标题】:difference between get('doctrine'); and getDoctrine();get('doctrine'); 之间的区别和 getDoctrine();
【发布时间】:2014-10-26 20:57:38
【问题描述】:
在 Symfony 中,我发现了以下三种访问学说服务和实体管理器的方法:
$em = $this->getDoctrine()->getManager();
$em = $this->get('doctrine')->getEntityManager();
$em = $this->container->get('doctrine.orm.entity_manager');
谁能解释一下它们的不同之处,并解释我们什么时候应该使用它们中的哪一个。
【问题讨论】:
标签:
symfony
service
doctrine-orm
entity
【解决方案1】:
第一个仅在扩展基本控制器时可用。这是做$this->get('doctrine')的捷径,你可以看到in the source:
public function getDoctrine()
{
if (!$this->container->has('doctrine')) {
throw new \LogicException('The DoctrineBundle is not registered in your application.');
}
return $this->container->get('doctrine');
}
$this->get('doctrine') 也仅在控制器中可用。 get也在基本控制器中定义,是$this->container->get()的快捷方式:
public function get($id)
{
return $this->container->get($id);
}
$this->container->get('doctrine') 是获取学说注册表的完整书面形式。
【解决方案2】:
$this->get('doctrine') its the method to use services,
在 symfony 中,你有调用这个服务的快捷方式$this->getDoctrine()