php - symfony 2 中搜索功能的最佳方式

标签 php symfony search-engine

我的网站上有一个使用elasticsearch实现的搜索功能。 现在我有一个关于设计的小问题。

我有一个 searchAction ,其路由 /search 接受查询字符串中的参数。就像/search?terms=...

我想让结果列表可过滤,但我对实现此目的的正确设计有一些疑问。

制作过滤结果列表的最佳解决方案是什么?

如果我传递过滤器参数,我需要使用当前 url 指定表单操作,并像链接一样附加当前查询字符串,对吧? 示例:

<form action="{{ current_pat }} ~ {{ query_string }}" method="post">
    <input type="checkbox" name="filter_one">....

在这种情况下,网址将类似于:/search?terms=... 并且在 $post 中我有过滤器。这是正确的解决方案,还是链接列表更好? 示例:

<ul>
    <li><a href="{{current_path}} ~ {{ query_string }} ~ {{ this_filter }}">... 
    <li><a href="{{current_path}} ~ {{ query_string }} ~ {{ this_another_filter }}">
...

在这种情况下,网址将类似于:/search?terms=...&this_filter=...

在具有获取参数和后置过滤器的表单情况下,我需要在搜索操作中采用这两种类型的参数。这样好吗?

现在,链接将包含 $get 请求中的所有参数,但我不喜欢在模板中使用查询字符串构建网址。

最好的方法是什么?

最佳答案

Use KNP paginator bundle with search action. On search form make the post action to your searchAction in Controller and after sorting data with matching criteria, re-render the page.

public function searchAction(Request $request,array $arguments = array())
        {
            $em = $this->getDoctrine()->getManager();
            $paginator = $this->get('knp_paginator');
            $parameter = $request->get('board_search');
            $boardRepo = $this->getDoctrine()->getRepository('PNCMISDashboardBundle:ExaminationBoards')->loadBoardByName($parameter);
            $boards = $paginator->paginate($boardRepo, $this->get('request')->query->get('page', 1), 10);
            return $this->render('PNCMISDashboardBundle:ExaminationBoards/CRUD:index.html.twig', array(
                    'boards'=> $boards,
                )
            );
        }


public function loadBoardByName($name)
    {
        $q = $this
            ->createQueryBuilder('boards')
            ->where('upper(boards.name) LIKE upper(:search)')
            ->setParameter('search', '%'.$name.'%')
            ->getQuery()
        ;
        try {
            // The Query::getSingleResult() method throws an exception
            // if there is no record matching the criteria.
            $user = $q->getResult();
        } catch (NoResultException $e) {
            throw new UsernameNotFoundException(sprintf('Unable to find an board identified by "%s".', $name), null, 0, $e);
        }
        return $user;
    }

关于php - symfony 2 中搜索功能的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14895904/

相关文章:

php - 准备好的语句无法通过引用传递参数 2。

php - mysql + json + iphone + 网络服务

php - 如何在 Controller 中正确执行 symfony2 中的 If 语句

php - 如何使用 IN 查询在 solr join 中执行多个条件?

mysql - Sphinx 搜索或 MySQL 庞大的数据库搜索引擎

php - MongoDB查询日期

php - 警告 : max() [function. 最大值] : Array must contain at least one element - whats is this?

php - 使用分页过滤REDIS中的在线用户

symfony - security.yml 中的未知实体命名空间与 Symfony 2 中的自定义实体文件夹

python - Django:icontains 大小写对 unicode 敏感