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.
source to share
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.
source to share