gpt4 book ai didi

symfony - get ('doctrine' 之间的区别);和 getDoctrine();

转载 作者:行者123 更新时间:2023-12-03 01:59:38 27 4
gpt4 key购买 nike

在 Symfony 中,我发现了三种访问学说服务和实体管理器的方法,如下所示:

$em = $this->getDoctrine()->getManager();

$em = $this->get('doctrine')->getEntityManager();

$em = $this->container->get('doctrine.orm.entity_manager');

任何人都可以解释一下它们之间的差异,并解释一下我们什么时候应该使用它们中的哪一个。

最佳答案

第一个仅在扩展基本 Controller 时可用。这是执行 $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') 也仅在 Controller 中可用。 get 也在基本 Controller 中定义,并且是 $this->container->get() 的快捷方式:

public function get($id)
{
return $this->container->get($id);
}

$this->container->get('doctrine') 是获取 Doctrine 注册表的完整书面形式。

关于symfony - get ('doctrine' 之间的区别);和 getDoctrine();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26577878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com