2014-10-26 77 views
3

在Symfony的,我发现访问学说服务和实体管理器如下三种方式:get('doctrine');和getDoctrine();

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

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

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

任何人都亲切地解释他们的区别,请和我们解释时,应使用哪个人。

回答

6

第一个仅在扩展基本控制器时可用。这是一个快捷方式做$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')是获得教义注册表的完全书面形式。

+0

谢谢沃特 – 2014-10-27 01:24:06

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

而且在symfony中你必须shortcutes调用此服务$this->getDoctrine(