2015-12-24 32 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(); 
       /../ 


     } 
相关问题