[英]How do I get total count for a search query in the controller in 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
错误:可捕获的致命错误:无法将类Knp \\ Bundle \\ PaginatorBundle \\ Pagination \\ SlidingPagination的对象转换为字符串
I've tried treating $pagination like an array, by requesting $pagination[totalCount], but that just returned null. 我尝试通过请求$ pagination [totalCount]来将$ pagination视为数组,但这只是返回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: []
}
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 实际上,
$pagination
是Knp\\Bundle\\PaginatorBundle\\Pagination\\SlidingPagination
,这是父类Knp\\Component\\Pager\\Pagination\\AbstractPagination
https://github.com/KnpLabs/knp-components/blob /master/src/Knp/Component/Pager/Pagination/AbstractPagination.php#L118:L121
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.