Difference between get ('doctrine'); and getDoctrine ();

In Symfony, I found three ways to access the doctrine service and entity manager:

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

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

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

      

Someone can explain their differences and explain when we will use them.

+3


source to share


2 answers


The first is only available when expanding the base controller. This is a shortcut to execute $this->get('doctrine')

, as 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')

also only available in controllers. get

also defined in the base controller and is a shortcut for $this->container->get()

:



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

      

$this->container->get('doctrine')

is a fully written form for obtaining a register of doctrine.

+6


source


$this->get('doctrine')  its the method to use services,

      



And in symfony you have shortcutes to call that service $this->getDoctrine(

)

+1


source







All Articles