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: []
}