Symfony – the difference between get (‘doctrine ‘); And getdoctrine();

In symfony, I found three ways to access the doctrine service and entity manager, as shown below:

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

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

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

Anyone can ask them to explain their differences and explain when and which one we should use

Solution

The first is only available when extending the base controller This is a shortcut to execute $this - > get ('doctrine '). You can see 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 ') can only be used in the controller Get is also defined in the basic controller and is a shortcut to $this - > container - > get():

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

$this - > container - > get ('doctrine ') is a complete written form for obtaining the doctrine registry

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>