2015-12-24 26 views
1

我需要創建一個狀態下拉列表,以便在我從國家drop_down選擇國家後,應該使用Ajax指出該國家的drop_down。 我收到錯誤消息。getDoctrine()不能在FOSUserBundle中工作symfony2

試圖調用名爲「getDoctrine」類的「FOS \ UserBundle \控制器\這個RegistrationController」

的AJAX獲取調用和國家的ID被傳遞給控制器​​一個未定義的方法,主要問題是getDoctrine函數。 這是我的控制器。

我是symfony的新手,請幫幫我。

public function getStateAction(Request $request) 
    {    
      $em1 = $this->getDoctrine()->getManager(); 
      $data = $request->request->all(); 

      $countryId = $data['id']; 
      $repository = $em->getRepository('FOSUserBundle:State'); 
      $query = $repository->createQueryBuilder('p'); 
      $query->where('p.country ='.$countryId); 
      $query->orderBy('p.id', 'ASC'); 
      $stateList = $query->getQuery()->getResult(); 
      //print_r($stateList); die; 
    } 

這裏是我的ajax

$(document).ready(function(){ 
    $("#fos_user_registration_form_country_id").change(function(){ 
    var countryId = $(this).val(); 
      if(countryId!=0){ 
       $.ajax({ 
       type: "POST", 
       url: "{{ path('fos_user_registration_country') }}", 
       data: {id: countryId}, 
       cache: false, 
       success: function(result){ 
       ("#fos_user_registration_form_state_id").append(result); 
      } 
      }); 
     } 
    }); 
}); 
+0

請更正您的語法並以更好的方式構建您的文本。 – kwoxer

回答

2

你嘗試:

public function getStateAction(Request $request) 
{ 
    $em1 = $this->container->get('doctrine')->getManager(); 

    /.../ 
} 

getDoctrine()是類Symfony\Bundle\FrameworkBundle\Controller\Controller誰不是從FOS\UserBundle\Controller\RegistrationController

3

我想擴展的方法您正在使用不是最新maste的FOSUserBundle版本r版本。您的問題是由於這樣的事實,直到開發主版本,RegistrationController擴展Symfony\Component\DependencyInjection\ContainerAware而不是Symfony\Bundle\FrameworkBundle\Controller\ControllerController類延伸ContainerAware幷包含一堆快捷方式調用,如getDoctrine,generateUrlisGranted

getDoctrine方法只是調用容器如..

/** 
* Shortcut to return the Doctrine Registry service. 
* 
* @return Registry 
* 
* @throws \LogicException If DoctrineBundle is not available 
*/ 
protected function getDoctrine() 
{ 
    if (!$this->container->has('doctrine')) { 
     throw new \LogicException('The DoctrineBundle is not registered in your application.'); 
    } 

    return $this->container->get('doctrine'); 
} 

你有2種選擇:getDoctrine方法複製到你的類,或者只是直接使用$this->container->get('doctrine')

0

我將此行添加到我的控制器,控制器從容器而不是控制器繼承。 Thankyou幫助@scoolnico。

public function getStateAction(Request $request) 
     {    
       $em = $this->getDoctrine()->getManager(); 
       /../ 


     } 
相關問題