Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms Symfony 2自动完成路由_Forms_Symfony_Autocomplete - Fatal编程技术网

Forms Symfony 2自动完成路由

Forms Symfony 2自动完成路由,forms,symfony,autocomplete,Forms,Symfony,Autocomplete,我安装了这个包:并尝试制作一个ajax自动完成。我的表格中有: $builder ->add('PermitsCompany', 'genemu_jqueryautocompleter_entity', array( 'route_name' => 'ajax_company', 'class' => 'MainCoreBundle:Company', )); 这在

我安装了这个包:并尝试制作一个ajax自动完成。我的表格中有:

$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:[语义错误]中的批注“@Route” 方法Main\CoreBundle\Controller\NewController::ajaxCompanyAction() 从来没有进口过。你有没有忘了为你的工作添加一个“使用”语句 这个注释


您需要在控制器顶部添加以下行:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
没有它,控制器就无法从注释中正确加载类

正确的JSON响应还应正确设置
内容类型

$response = new Response(json_encode($json));
$response->headers->set('Content-Type', 'application/json');
return $response;
根据,你的建造者有点不正确。改用这个:

$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,
));
您尝试使用的捆绑包看起来不像是一个完整的解决方案,也不打算成为一个完整的解决方案:

这些实现中可能有一些bug,这个包只是一个表单类型的概念,它对Symfony2项目非常有用


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

现在错误消失了,谢谢:)但是你知道为什么自动完成仍然不起作用吗?我输入了一个空文本,但当我键入某个内容时,它不会自动完成…这是路由问题吗?@Cre3k您应该检查开发人员工具或Firebug中的控制台和网络选项卡,具体取决于您使用的浏览器。您是否尝试直接在浏览器中加载
/ajax\u company
路由?未定义的方法“findAjaxValue”。@Cre3k因此您应该从那里开始。。。没有前端代码我帮不了你