Skip to content

Commit

Permalink
Fixed KNP pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
pmishev committed Dec 4, 2015
1 parent 857ff81 commit f98c551
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Paginator/KnpPaginatorAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
22 changes: 21 additions & 1 deletion Subscriber/KnpPaginateQuerySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit f98c551

Please sign in to comment.