dongzha5934 2018-05-15 19:01
浏览 70
已采纳

如何在Symfony的控制器中获取搜索查询的总计数

Use case: I want to store the search query and the total number of results to the db so I can see what people are searching for that doesn't exist in my application.

What is working: I'm able to get and store the query, but I can't figure out how to get the total number of results for the search.

Here is the code sample from the Controller. When I try this currently, I get the following

error:Catchable Fatal Error: Object of class Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination could not be converted to string

I've tried treating $pagination like an array, by requesting $pagination[totalCount], but that just returned null.

 public function fpcAction(Request $request)
    {

            $query = dump($request->query->get('q'));

            $finder = $this->container->get('fos_elastica.finder.app.product');
            $page = $request->query->getInt('page', 1);

            $paginator = $this->get('knp_paginator');
            $results = $finder->createPaginatorAdapter($query);
            $pagination = $paginator->paginate($results, $page, 12);



            $searchmetrics = new SearchTerms();
            $searchmetrics->setSearchterm($query);
            $searchmetrics->setDate(time());

            // TODO: Need to get the total qty of search results for this specific query
            $searchmetrics->setResultsqty($pagination);

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($searchmetrics);
            $entityManager->flush();


            return $this->render('default/search.html.twig', ['searchresults' => $pagination, 'query' => $query]);

    }

Here is a dump of the variable as it outputs on the twig template:

 "searchresults" => SlidingPagination {#1028 ▼
    -route: "search"
    -params: array:1 [▶]
    -pageRange: 5
    -template: "@KnpPaginator/Pagination/sliding.html.twig"
    -sortableTemplate: "@KnpPaginator/Pagination/sortable_link.html.twig"
    -filtrationTemplate: "@KnpPaginator/Pagination/filtration.html.twig"
    #currentPageNumber: 1
    #numItemsPerPage: 12
    #items: array:12 [▶]
    #totalCount: 8104
    #paginatorOptions: array:6 [▶]
    #customParameters: []
  }
  • 写回答

1条回答 默认 最新

  • dspv70887 2018-05-15 19:38
    关注

    You should do

    $searchmetrics->setResultsqty($pagination->getTotalItemCount());
    

    In fact, $pagination is of class Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination and this is the code of the parent class Knp\Component\Pager\Pagination\AbstractPagination https://github.com/KnpLabs/knp-components/blob/master/src/Knp/Component/Pager/Pagination/AbstractPagination.php#L118:L121

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移
  • ¥15 如何用python处理excel的数据(极值标准化)