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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么