gpt4 book ai didi

php - getDoctrine() 在 FOSUserBundle symfony2 中不起作用

转载 作者:行者123 更新时间:2023-12-01 06:44:57 25 4
gpt4 key购买 nike

我需要创建一个州的下拉列表,这样一旦我从国家 drop_down 中选择国家,那么该国家的州 drop_down 就应该使用 Ajax 出现。我收到以下错误后的错误。

Attempted to call an undefined method named "getDoctrine" of class "FOS\UserBundle\Controller\RegistrationController"

AJAX 被调用,国家的 id 被传递到 Controller ,主要问题是 getDoctrine 函数。这是我的 Controller 。

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

最佳答案

我假设您使用的 FOSUserBundle 版本不是最新的主版本。您的问题是由于这样的事实,直到开发主版本为止,RegistrationController扩展Symfony\Component\DependencyInjection\ContainerAware而不是Symfony\Bundle\FrameworkBundle\Controller\ControllerController类(class)扩展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')直接。

关于php - getDoctrine() 在 FOSUserBundle symfony2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34448268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com