Php getDoctrine()在FOSUserBundle symfony2中不工作
我需要创建一个州下拉列表,这样一旦我从“国家”下拉列表中选择“国家”,那么该国家的“州”下拉列表就应该使用Ajax。 我在错误之后得到错误 试图调用类“FOS\UserBundle\Controller\RegistrationController”中名为“getDoctrine”的未定义方法 AJAX被调用,国家的id被传递给控制器,主要问题是Php getDoctrine()在FOSUserBundle symfony2中不工作,php,jquery,ajax,symfony,Php,Jquery,Ajax,Symfony,我需要创建一个州下拉列表,这样一旦我从“国家”下拉列表中选择“国家”,那么该国家的“州”下拉列表就应该使用Ajax。 我在错误之后得到错误 试图调用类“FOS\UserBundle\Controller\RegistrationController”中名为“getDoctrine”的未定义方法 AJAX被调用,国家的id被传递给控制器,主要问题是getdoctor函数。 这是我的控制器 我是新来的,请帮帮我 public function getStateAction(Request $requ
getdoctor
函数。
这是我的控制器
我是新来的,请帮帮我
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);
}
});
}
});
});
您是否尝试过:
public function getStateAction(Request $request)
{
$em1 = $this->container->get('doctrine')->getManager();
/.../
}
getDoctrine()
是类Symfony\Bundle\FrameworkBundle\Controller\Controller
的一种方法,它不是从FOS\UserBundle\Controller\RegistrationController
扩展而来的。您是否尝试过:
public function getStateAction(Request $request)
{
$em1 = $this->container->get('doctrine')->getManager();
/.../
}
getDoctrine()
是类Symfony\Bundle\FrameworkBundle\Controller\Controller
的一种方法,它不是从FOS\UserBundle\Controller\RegistrationController
扩展而来的。我假设您使用的FOSUserBundle版本不是最新的主版本。您的问题是因为在开发主版本之前,RegistrationController
扩展了Symfony\Component\DependencyInjection\containerware
,而不是Symfony\Bundle\FrameworkBundle\Controller\Controller
。Controller
类扩展了containerware
,并包含一系列快捷调用,如getDoctrine
、generateUrl
和已被授予
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');
}
您有两种选择:将
getDoctrine
方法复制到类中,或者直接使用$this->container->get('doctrine')
。我假设您使用的是一个不是最新主版本的FOSUserBundle版本。您的问题是因为在开发主版本之前,RegistrationController
扩展了Symfony\Component\DependencyInjection\containerware
,而不是Symfony\Bundle\FrameworkBundle\Controller\Controller
。Controller
类扩展了containerware
,并包含一系列快捷调用,如getDoctrine
、generateUrl
和已被授予
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');
}
您有两个选项:将
getDoctrine
方法复制到类中,或者直接使用$this->container->get('doctrine')
。我将这一行添加到我的控制器中,控制器从容器而不是控制器继承。谢谢你的帮助@scoolnico
public function getStateAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
/../
}
我将这一行添加到我的控制器中,控制器从容器而不是控制器继承。谢谢你的帮助@scoolnico
public function getStateAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
/../
}
请更正语法并以更好的方式组织文本。请更正语法并以更好的方式组织文本。