From f98c551ba2bfd8d8357d0c0f1b823232a192374f Mon Sep 17 00:00:00 2001 From: pmishev Date: Fri, 4 Dec 2015 13:47:46 +0000 Subject: [PATCH] Fixed KNP pagination --- Paginator/KnpPaginatorAdapter.php | 8 ++++++++ Subscriber/KnpPaginateQuerySubscriber.php | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Paginator/KnpPaginatorAdapter.php b/Paginator/KnpPaginatorAdapter.php index 9e6e83b..2b5679d 100644 --- a/Paginator/KnpPaginatorAdapter.php +++ b/Paginator/KnpPaginatorAdapter.php @@ -56,6 +56,14 @@ public function __construct(Finder $finder, array $documentClasses, array $searc $this->additionalRequestParams = $additionalRequestParams; } + /** + * @return int + */ + public function getResultsType() + { + return $this->resultsType; + } + /** * Return results for this page only * diff --git a/Subscriber/KnpPaginateQuerySubscriber.php b/Subscriber/KnpPaginateQuerySubscriber.php index 793b118..0d08ce0 100644 --- a/Subscriber/KnpPaginateQuerySubscriber.php +++ b/Subscriber/KnpPaginateQuerySubscriber.php @@ -3,6 +3,8 @@ namespace Sineflow\ElasticsearchBundle\Subscriber; use Knp\Component\Pager\Event\ItemsEvent; +use Sineflow\ElasticsearchBundle\Exception\Exception; +use Sineflow\ElasticsearchBundle\Finder\Finder; use Sineflow\ElasticsearchBundle\Paginator\KnpPaginatorAdapter; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; @@ -36,7 +38,25 @@ public function items(ItemsEvent $event) /** @var $results PartialResultsInterface */ $results = $event->target->getResults($event->getOffset(), $event->getLimit(), $sortField, $sortDirection); $event->count = $event->target->getTotalHits(); - $event->items = $results; + + $resultsType = $event->target->getResultsType(); + switch ($resultsType) { + case Finder::RESULTS_OBJECT: + $event->items = iterator_to_array($results); + break; + + case Finder::RESULTS_ARRAY: + $event->items = $results; + break; + + case Finder::RESULTS_RAW: + $event->items = $results['hits']['hits']; + break; + + default: + throw new Exception(sprintf('Unsupported results type "%s" for KNP paginator', $resultsType)); + } + $event->stopPropagation(); } }