2014-10-21 106 views
0

我安装了这个软件包:GenemuFormBudnle,我尝试做一个ajax自动完成。我有这个在我的表格:Symfony 2自动完成路由

$builder 
      ->add('PermitsCompany', 'genemu_jqueryautocompleter_entity', array(
       'route_name' => 'ajax_company', 
       'class' => 'MainCoreBundle:Company', 
      )); 

这在我的控制器:NewController.php

/** 
    * @Route("/ajax_company", name="ajax_company") 
    */ 
    public function ajaxCompanyAction(Request $request) 
    { 
     $value = $request->get('id'); 

     $permits = $this->getDoctrine()->getRepository('JokerCoreBundle:Company')->findAjaxValue($value); 


     $json = array(); 
     foreach ($permits as $permit) { 
      $json[] = array(
       'label' => $permit->getName(), 
       'value' => $permit->getId() 
      ); 
     } 

     $response = new Response(); 
     $response->setContent(json_encode($json)); 

     return $response; 
    } 

而这在我的路线:

ajax_company: 
    defaults: { _controller: MainCoreBundle:Permits:ajaxCompany} 
    pattern: /ajax_company/ 
    type:  annotation 

下面是一个错误信息:

AnnotationException:[语义错误]在 方法主要\ CoreBundle \控制器\ NewController :: ajaxCompanyAction() 注释 “@Route” 从未被导入。你可能忘记为这个注释添加一个“使用”语句 ?

回答

1

你需要下面的行添加到您的控制器的顶部:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 

没有它,控制器不必从注释正确加载类的能力。

一个适当的JSON响应也应该有Content-Type设置正确:

$response = new Response(json_encode($json)); 
$response->headers->set('Content-Type', 'application/json'); 
return $response; 

these docs,你的建设者稍有不正确。使用这个来代替:

$builder 
    ->add('PermitsCompany', 'genemu_jqueryautocompleter_entity', array(
     'route_name' => 'ajax_company', 
     'class' => 'MainCoreBundle\Entity\Company', // Must use namespace here with slashes 
    )) 
; 

我已经检查了GenemuFormBundle存储库,它看起来像不提供或与束注入到你的实体仓库一个findAjaxValue功能。您必须在您的存储库中创建的findAjaxValue功能或恢复到一个辅助功能,如findBy像这样:

$permits = $this->getDoctrine()->getRepository('JokerCoreBundle:Company')->findBy(array(
    'name' => $value, 
)); 

你正在尝试使用看起来并不像一个完整的解决方案包,也不会打算成为一个:

在这些实现中可能有一些错误,这个包只是一个表单类型的想法,对于Symfony2项目可能非常有用。

也许你应该记住这一点,并试着想出自己的解决方案,或找到一个替代捆绑。

+0

现在错误消失感谢:)但你有什么想法为什么autocomplete仍然没有工作?我得到一个空的文本输入,但是当我输入的东西不会自动完成它... – Cre3k 2014-10-21 21:32:38

+0

这是一个路线问题? – Cre3k 2014-10-21 21:33:27

+0

@ Cre3k您应该检查开发工具或Firebug中的控制台和网络选项卡,具体取决于您使用的浏览器。您是否尝试直接在浏览器中加载'/ ajax_company'路线? – sjagr 2014-10-21 21:34:32