Added knp paginator sort functionality to PaginateElasticaQuerySubscriber
This commit is contained in:
parent
13c2d10e39
commit
1bc148569b
|
@ -85,10 +85,10 @@
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="fos_elastica.paginator.subscriber" class="FOS\ElasticaBundle\Subscriber\PaginateElasticaQuerySubscriber">
|
<service id="fos_elastica.paginator.subscriber" class="FOS\ElasticaBundle\Subscriber\PaginateElasticaQuerySubscriber">
|
||||||
<tag name="knp_paginator.subscriber" />
|
<call method="setRequest">
|
||||||
<call method="setContainer">
|
<argument type="service" id="request" on-invalid="null" strict="false" />
|
||||||
<argument type="service" id="service_container" />
|
|
||||||
</call>
|
</call>
|
||||||
|
<tag name="knp_paginator.subscriber" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="fos_elastica.property_accessor" class="%fos_elastica.property_accessor.class%" />
|
<service id="fos_elastica.property_accessor" class="%fos_elastica.property_accessor.class%" />
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace FOS\ElasticaBundle\Subscriber;
|
namespace FOS\ElasticaBundle\Subscriber;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
use Knp\Component\Pager\Event\ItemsEvent;
|
use Knp\Component\Pager\Event\ItemsEvent;
|
||||||
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
|
use FOS\ElasticaBundle\Paginator\PaginatorAdapterInterface;
|
||||||
|
@ -9,19 +10,18 @@ use FOS\ElasticaBundle\Paginator\PartialResultsInterface;
|
||||||
|
|
||||||
class PaginateElasticaQuerySubscriber implements EventSubscriberInterface
|
class PaginateElasticaQuerySubscriber implements EventSubscriberInterface
|
||||||
{
|
{
|
||||||
private $container;
|
private $request;
|
||||||
|
|
||||||
public function setContainer($container)
|
public function setRequest(Request $request = null)
|
||||||
{
|
{
|
||||||
$this->container = $container;
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function items(ItemsEvent $event)
|
public function items(ItemsEvent $event)
|
||||||
{
|
{
|
||||||
if ($event->target instanceof PaginatorAdapterInterface) {
|
if ($event->target instanceof PaginatorAdapterInterface) {
|
||||||
|
|
||||||
// Add sort to query
|
// Add sort to query
|
||||||
$this->addPagingSort($event);
|
$this->setSorting($event);
|
||||||
|
|
||||||
/** @var $results PartialResultsInterface */
|
/** @var $results PartialResultsInterface */
|
||||||
$results = $event->target->getResults($event->getOffset(), $event->getLimit());
|
$results = $event->target->getResults($event->getOffset(), $event->getLimit());
|
||||||
|
@ -40,19 +40,17 @@ class PaginateElasticaQuerySubscriber implements EventSubscriberInterface
|
||||||
/**
|
/**
|
||||||
* Adds knp paging sort to query
|
* Adds knp paging sort to query
|
||||||
*
|
*
|
||||||
* @param ItemsEvent $event
|
* @param ItemsEvent $event
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
protected function addPagingSort(ItemsEvent $event)
|
protected function setSorting(ItemsEvent $event)
|
||||||
{
|
{
|
||||||
$request = $this->container->get('request');
|
|
||||||
$options = $event->options;
|
$options = $event->options;
|
||||||
$sortField = $request->get($options['sortFieldParameterName']);
|
$sortField = $this->request->get($options['sortFieldParameterName']);
|
||||||
|
|
||||||
if (!empty($sortField)) {
|
if (!empty($sortField)) {
|
||||||
// determine sort direction
|
// determine sort direction
|
||||||
$dir = 'asc';
|
$dir = 'asc';
|
||||||
$sortDirection = $request->get($options['sortDirectionParameterName']);
|
$sortDirection = $this->request->get($options['sortDirectionParameterName']);
|
||||||
if ('desc' === strtolower($sortDirection)) {
|
if ('desc' === strtolower($sortDirection)) {
|
||||||
$dir = 'desc';
|
$dir = 'desc';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue