vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/DBALQueryBuilderSubscriber.php line 16

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Doctrine\DBAL\Query\QueryBuilder;
  6. /**
  7.  * DBALQueryBuilderSubscriber.php
  8.  *
  9.  * @author Vladimir Chub <v@chub.com.ua>
  10.  */
  11. class DBALQueryBuilderSubscriber implements EventSubscriberInterface
  12. {
  13.     public function items(ItemsEvent $event)
  14.     {
  15.         if ($event->target instanceof QueryBuilder) {
  16.             /** @var $target QueryBuilder */
  17.             $target $event->target;
  18.         
  19.             // count results
  20.             $qb = clone $target;
  21.             
  22.             //reset count orderBy since it can break query and slow it down
  23.             $qb
  24.                 ->resetQueryPart('orderBy')
  25.             ;
  26.             
  27.             // get the query
  28.             $sql $qb->getSQL();
  29.             
  30.             $qb
  31.                 ->resetQueryParts()
  32.                 ->select('count(*) as cnt')
  33.                 ->from('(' $sql ')''dbal_count_tbl')
  34.             ;
  35.             $event->count $qb
  36.                 ->execute()
  37.                 ->fetchColumn(0)
  38.             ;
  39.             // if there is results
  40.             $event->items = array();
  41.             if ($event->count) {
  42.                 $qb = clone $target;
  43.                 $qb
  44.                     ->setFirstResult($event->getOffset())
  45.                     ->setMaxResults($event->getLimit())
  46.                 ;
  47.                 
  48.                 $event->items $qb
  49.                     ->execute()
  50.                     ->fetchAll()
  51.                 ;
  52.             }
  53.             
  54.             $event->stopPropagation();
  55.         }
  56.     }
  57.     public static function getSubscribedEvents()
  58.     {
  59.         return array(
  60.             'knp_pager.items' => array('items'10 /*make sure to transform before any further modifications*/)
  61.         );
  62.     }
  63. }