vendor/knplabs/knp-components/src/Knp/Component/Pager/Event/Subscriber/Paginate/Doctrine/ODM/MongoDB/QuerySubscriber.php line 11

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ODM\MongoDB;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Knp\Component\Pager\Event\ItemsEvent;
  5. use Doctrine\ODM\MongoDB\Query\Query;
  6. class QuerySubscriber implements EventSubscriberInterface
  7. {
  8.     public function items(ItemsEvent $event)
  9.     {
  10.         if ($event->target instanceof Query) {
  11.             // items
  12.             $type $event->target->getType();
  13.             if ($type !== Query::TYPE_FIND) {
  14.                 throw new \UnexpectedValueException('ODM query must be a FIND type query');
  15.             }
  16.             static $reflectionProperty;
  17.             if (is_null($reflectionProperty)) {
  18.                 $reflectionClass = new \ReflectionClass('Doctrine\MongoDB\Query\Query');
  19.                 $reflectionProperty $reflectionClass->getProperty('query');
  20.                 $reflectionProperty->setAccessible(true);
  21.             }
  22.             $queryOptions $reflectionProperty->getValue($event->target);
  23.             $queryOptions['limit'] = $event->getLimit();
  24.             $queryOptions['skip'] = $event->getOffset();
  25.             $resultQuery = clone $event->target;
  26.             $reflectionProperty->setValue($resultQuery$queryOptions);
  27.             $cursor $resultQuery->execute();
  28.             // set the count from the cursor
  29.             $event->count $cursor->count();
  30.             $event->items = array();
  31.             // iterator_to_array for GridFS results in 1 item
  32.             foreach ($cursor as $item) {
  33.                 $event->items[] = $item;
  34.             }
  35.             $event->stopPropagation();
  36.         }
  37.     }
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return array(
  41.             'knp_pager.items' => array('items'0)
  42.         );
  43.     }
  44. }