【问题标题】:getDoctrine() not working in FOSUserBundle symfony2getDoctrine() 在 FOSUserBundle symfony2 中不起作用
【发布时间】:2016-03-30 15:09:35
【问题描述】:

我需要创建一个状态下拉列表,这样一旦我从国家/地区下拉列表中选择国家/地区,那么该国家/地区的状态下拉列表应该使用 Ajax 出现。 出现错误后出现错误。

试图调用“FOS\UserBundle\Controller\RegistrationController”类的名为“getDoctrine”的未定义方法

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);
             }
           });
        }
    });
});

【问题讨论】:

  • 请更正您的语法并以更好的方式组织您的文本。

标签: php jquery ajax symfony


【解决方案1】:

我将此行添加到我的控制器中,并且控制器是从容器而不是控制器继承的。感谢@scoolnico 的帮助。

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


        }

【讨论】:

    【解决方案2】:

    我假设您使用的 FOSUserBundle 版本不是最新的主版本。您的问题是因为在 dev-master 版本之前,RegistrationController 扩展了 Symfony\Component\DependencyInjection\ContainerAware 而不是 Symfony\Bundle\FrameworkBundle\Controller\ControllerController 类扩展了 ContainerAware 并包含一堆快捷方式调用,例如 getDoctrinegenerateUrlisGranted

    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')

    【讨论】:

      【解决方案3】:

      您是否尝试过:

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

      getDoctrine() 是类Symfony\Bundle\FrameworkBundle\Controller\Controller 的一个方法,它不是从FOS\UserBundle\Controller\RegistrationController 扩展而来的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-17
        • 1970-01-01
        • 2018-06-23
        • 1970-01-01
        相关资源
        最近更新 更多