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

如何在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条回答 默认 最新

相关推荐 更多相似问题