mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 09:02:12 +00:00
added sorting.
This commit is contained in:
parent
72682dc1cc
commit
b525265d88
8 changed files with 114 additions and 18 deletions
|
|
@ -32,8 +32,10 @@ class AddressController extends ContainerAware
|
|||
public function listAction()
|
||||
{
|
||||
$addressManager = $this->container->get('sylius_addressing.manager.address');
|
||||
$paginator = $addressManager->createPaginator();
|
||||
|
||||
$addressSorter = $this->container->get('sylius_addressing.sorter.address');
|
||||
|
||||
$paginator = $addressManager->createPaginator($addressSorter);
|
||||
$paginator->setCurrentPage($this->container->get('request')->query->get('page', 1), true, true);
|
||||
|
||||
$addresses = $paginator->getCurrentPageResults();
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ use Doctrine\ORM\EntityManager;
|
|||
use Doctrine\ORM\EntityRepository;
|
||||
use Sylius\Bundle\AddressingBundle\Model\AddressInterface;
|
||||
use Sylius\Bundle\AddressingBundle\Model\AddressManager as BaseAddressManager;
|
||||
use Sylius\Bundle\AddressingBundle\Sorting\SorterInterface;
|
||||
|
||||
/**
|
||||
* Address entity manager.
|
||||
|
|
@ -115,11 +116,17 @@ class AddressManager extends BaseAddressManager
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createPaginator()
|
||||
public function createPaginator(SorterInterface $sorter = null)
|
||||
{
|
||||
$queryBuilder = $this->entityManager->createQueryBuilder()
|
||||
->select('a')
|
||||
->from($this->class, 'a');
|
||||
->from($this->class, 'a')
|
||||
->orderBy('a.createdAt', 'DESC')
|
||||
;
|
||||
|
||||
if (null !== $sorter) {
|
||||
$sorter->sort($queryBuilder);
|
||||
}
|
||||
|
||||
return new Pagerfanta(new DoctrineORMAdapter($queryBuilder->getQuery()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,11 @@ abstract class Address implements AddressInterface
|
|||
protected $createdAt;
|
||||
protected $updatedAt;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->incrementCreatedAt();
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
|
|
|
|||
|
|
@ -42,12 +42,4 @@ abstract class AddressManager implements AddressManagerInterface
|
|||
{
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setClass($class)
|
||||
{
|
||||
$this->class = $class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,8 @@
|
|||
|
||||
namespace Sylius\Bundle\AddressingBundle\Model;
|
||||
|
||||
use Sylius\Bundle\AddressingBundle\Sorting\SorterInterface;
|
||||
|
||||
/**
|
||||
* Address manager interface.
|
||||
*
|
||||
|
|
@ -24,6 +26,13 @@ interface AddressManagerInterface
|
|||
* @return AddressInterface
|
||||
*/
|
||||
function createAddress();
|
||||
|
||||
/**
|
||||
* Creates paginator.
|
||||
*
|
||||
* @param SorterInterface $sorter
|
||||
*/
|
||||
function createPaginator(SorterInterface $sorter = null);
|
||||
|
||||
/**
|
||||
* Persists address model.
|
||||
|
|
@ -73,11 +82,4 @@ interface AddressManagerInterface
|
|||
* @return string The address model class
|
||||
*/
|
||||
function getClass();
|
||||
|
||||
/**
|
||||
* Sets the address model class.
|
||||
*
|
||||
* @param string $class The address model class
|
||||
*/
|
||||
function setClass($class);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,13 +17,22 @@
|
|||
|
||||
<parameters>
|
||||
<parameter key="sylius_addressing.manager.address.class">Sylius\Bundle\AddressingBundle\Entity\AddressManager</parameter>
|
||||
<parameter key="sylius_addressing.sorter.address.class">Sylius\Bundle\AddressingBundle\Sorting\ORM\AddressSorter</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<!-- managers... -->
|
||||
<service id="sylius_addressing.manager.address" class="%sylius_addressing.manager.address.class%">
|
||||
<argument type="service" id="doctrine.orm.default_entity_manager" />
|
||||
<argument>%sylius_addressing.model.address.class%</argument>
|
||||
</service>
|
||||
|
||||
<!-- sorters... -->
|
||||
<service id="sylius_addressing.sorter.address" class="%sylius_addressing.sorter.address.class%">
|
||||
<call method="setContainer">
|
||||
<argument type="service" id="service_container" />
|
||||
</call>
|
||||
</service>
|
||||
</services>
|
||||
|
||||
</container>
|
||||
|
|
|
|||
57
src/Sylius/Bundle/AddressingBundle/Sorting/ORM/AddressSorter.php
Executable file
57
src/Sylius/Bundle/AddressingBundle/Sorting/ORM/AddressSorter.php
Executable file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\AddressingBundle\Sorting\ORM;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerAware;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Sylius\Bundle\AddressingBundle\Sorting\SorterInterface;
|
||||
|
||||
/**
|
||||
* Default ORM sorter.
|
||||
* Sorts address entities.
|
||||
*
|
||||
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
||||
*/
|
||||
class AddressSorter extends ContainerAware implements SorterInterface
|
||||
{
|
||||
public function sort($sortable)
|
||||
{
|
||||
if (!$sortable instanceof QueryBuilder) {
|
||||
throw new InvalidArgumentException('Default sorter supports only "Doctrine\\ORM\\QueryBuilder" as sortable argument.');
|
||||
}
|
||||
|
||||
$request = $this->container->get('request');
|
||||
|
||||
if (null === $sortProperty = $request->query->get('sort', null)) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$sortOrder = $request->query->get('order', 'ASC');
|
||||
|
||||
if (!in_array($sortOrder, array('ASC', 'DESC'))) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$addressClass = $this->container->getParameter('sylius_addressing.model.address.class');
|
||||
$reflectionClass = new \ReflectionClass($addressClass);
|
||||
|
||||
if (!in_array($sortProperty, array_keys($reflectionClass->getDefaultProperties()))) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var QueryBuilder */
|
||||
$sortable->orderBy('a.' . $sortProperty, $sortOrder);
|
||||
}
|
||||
}
|
||||
22
src/Sylius/Bundle/AddressingBundle/Sorting/SorterInterface.php
Executable file
22
src/Sylius/Bundle/AddressingBundle/Sorting/SorterInterface.php
Executable file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Paweł Jędrzejewski
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Sylius\Bundle\AddressingBundle\Sorting;
|
||||
|
||||
/**
|
||||
* Interface for sorter.
|
||||
*
|
||||
* @author Paweł Jędrzejewski <pjedrzejewski@diweb.pl>
|
||||
*/
|
||||
interface SorterInterface
|
||||
{
|
||||
function sort($sortable);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue