2014-04-03 24 views
1

當我重寫FOSUserBundle的ProfileController並添加這些行:Symfony2中 - 調用未定義的方法getDoctrine()覆蓋FOSUserBundle的ProfileController可

$em = $this->getDoctrine()->getManager(); 
    $resultat = $em->getRepository('PublishDemandsBundle:Demands')->findAll(); 

我收到以下錯誤:

Call to undefined method Register\UserBundle\Controller\ProfileController::getDoctrine() in ProfileController.php.

+2

你的ProfileContr怎麼樣oller看起來像?看起來你沒有擴展默認的控制器類。 –

+2

FOSUserBundle的ProfileController擴展['ContainerAware'](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/ProfileController.php#L29)而不是['Controller'](https://github.com/的symfony/symfony的/斑點/主/ SRC/Symfony的/捆綁/ FrameworkBundle /控制器/ Controller.php這樣)。因此,您需要:使用'$ this-> container-> get('doctrine')'或添加['getDoctrine()'](https://github.com/symfony/symfony/blob/master/src /Symfony/Bundle/FrameworkBundle/Controller/Controller.php#L218)自己重寫到ProfileController。 – nifr

回答

3

添加這個別名中方法給控制器修正了問題:

public function getDoctrine() 
{ 
    return $this->container->get('doctrine'); 
} 
相關問題