2014-10-26 69 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(