private function createCriteria()
 {
     $criteria = new Criteria();
     $criteria->orderBy(array('username' => 'ASC'));
     $criteria->setFirstResult(2);
     $criteria->setMaxResults(3);
     return $criteria;
 }
 /**
  * Initialize mocks for Doctrine
  * 
  * @param array $managersToMock List of managers to be mocked
  * 
  * @return void
  */
 protected function initDatabaseMock($managersToMock)
 {
     if (is_null($this->mockedManager)) {
         $test = $this;
         foreach ($managersToMock as $manager) {
             $entityName = $manager['entityName'];
             // EntityManager mock
             $entityManagerMock = new \mock\Doctrine\ORM\EntityManager();
             // ClassMetadata mock
             $classMetadata = new \mock\Doctrine\ORM\Mapping\ClassMetadata($entityName);
             $entityClassName = $manager['entityClassName'];
             $this->calling($classMetadata)->getName = function () use($entityClassName) {
                 return $entityClassName;
             };
             // EntityRepository mock
             $entityRepositoryMock = new \mock\Doctrine\ORM\EntityRepository($entityManagerMock, $classMetadata);
             $this->calling($entityRepositoryMock)->find = function ($id) use($test, $entityName) {
                 if (!empty($test->database[$entityName]) && array_key_exists($id, $test->database[$entityName])) {
                     return clone $test->database[$entityName][$id];
                 }
                 return null;
             };
             $this->calling($entityRepositoryMock)->findBy = function ($criteria = [], $sort = null, $limit = null, $start = 0) use($test, $entityName) {
                 $entities = new ArrayCollection($test->database[$entityName]);
                 $crit = new Criteria();
                 foreach ($criteria as $field => $value) {
                     $crit->andWhere($crit->expr()->eq($field, $value));
                 }
                 if (!is_null($sort)) {
                     $crit->orderBy($sort);
                 }
                 $crit->setFirstResult($start);
                 $crit->setMaxResults($limit);
                 return $entities->matching($crit)->map(function ($item) {
                     return clone $item;
                 });
             };
             // Overload main EntityManager functions
             $this->calling($entityManagerMock)->getRepository = function () use($entityRepositoryMock) {
                 return $entityRepositoryMock;
             };
             $this->calling($entityManagerMock)->getClassMetadata = function ($entity) use($classMetadata) {
                 return $classMetadata;
             };
             $this->calling($entityManagerMock)->persist = function ($entity) use($test, $entityName) {
                 if (!$entity->getId()) {
                     if (!empty($test->database[$entityName])) {
                         $entity->setId(count($test->database[$entityName]) + 1);
                     } else {
                         $entity->setId(1);
                     }
                 }
                 $test->database[$entityName][$entity->getId()] = $entity;
                 return true;
             };
             $this->calling($entityManagerMock)->remove = function ($entity) use($test, $entityName) {
                 if (!$entity->getId() || empty($test->database[$entityName][$entity->getId()])) {
                     return false;
                 }
                 unset($test->database[$entityName][$entity->getId()]);
                 return true;
             };
             $mockClass = '\\mock' . $manager['className'];
             $managerMock = new $mockClass($entityManagerMock, $entityName);
             $this->mockedManager[$manager['serviceName']] = $managerMock;
         }
     }
 }
 /**
  * Get items
  * @author Mohamed Labib <*****@*****.**>
  * 
  * @access public
  * 
  * @param int $offset
  * @param int $itemCountPerPage
  * @return array items queried
  */
 public function getItems($offset, $itemCountPerPage)
 {
     $this->setCriteria();
     $this->criteria->setFirstResult($offset);
     $this->criteria->setMaxResults($itemCountPerPage);
     return $this->query->filter($this->entityName, $this->criteria);
     //($pageNumber-1) for zero based count
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $indexName = $input->getOption('index');
     $batchSize = $input->getOption('batch_size');
     $indexManager = $this->getIndexManager($indexName);
     $totalDocuments = $indexManager->getTotalEntities();
     $iterations = $this->getIterations($totalDocuments, $batchSize);
     $output->writeln(sprintf('<info>Reindexing</info> "%s"', $indexName));
     $output->writeln(sprintf('<comment>Total documents:</comment> %s', $totalDocuments));
     $output->writeln(sprintf('<comment>Batch size:</comment> %s', $batchSize));
     $output->writeln(sprintf('<comment>Iterations:</comment> %s', $iterations));
     $progress = new ProgressBar($output, $totalDocuments);
     $progress->setFormat('verbose');
     $progress->setRedrawFrequency($batchSize);
     $progress->start();
     $indexManager->purgeIndex();
     for ($i = 0; $i < $iterations; $i++) {
         $criteria = new Criteria();
         $criteria->setMaxResults($batchSize);
         $criteria->setFirstResult($i * $batchSize);
         $collection = $indexManager->getEntitiesCollection($criteria);
         $collection->map(function (EntityInterface $entity) use($indexManager, $progress) {
             $indexManager->addEntity($entity);
             $progress->advance();
         });
     }
     $progress->finish();
     $output->writeln('');
     $output->writeln(sprintf('<info>Optimizing "%s"</info>', $indexName));
     $indexManager->optimizeIndex();
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function apply(Criteria $criteria, FilterValue $value = null)
 {
     $val = null !== $value ? $value->getValue() : $this->getDefaultValue();
     if (null !== $val) {
         $pageSize = $criteria->getMaxResults();
         if (null !== $pageSize) {
             $criteria->setFirstResult(QueryUtils::getPageOffset($val, $pageSize));
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getData(RequestInterface $request)
 {
     $criteria = new Criteria();
     $criteria->setMaxResults($request->getLimit());
     $criteria->setFirstResult($request->getOffset());
     $this->processOrder($criteria, $request);
     $this->processSearch($criteria, $request);
     $this->processGlobalSearch($criteria, $request);
     $result = $this->collection->matching($criteria);
     return $this->processData($result);
 }
Example #7
0
 public function findAllPublic($page, $itemPerPage)
 {
     $criteria = new Criteria();
     $criteria->where($criteria->expr()->eq('r.published', true));
     $criteria->orderBy(['r.dateUpdated' => 'DESC']);
     $criteria->setFirstResult(($page - 1) * $itemPerPage);
     $criteria->setMaxResults($itemPerPage);
     /** @var QueryBuilder $queryBuilder */
     $queryBuilder = $this->objectRepository->createQueryBuilder('r');
     $queryBuilder->addCriteria($criteria);
     return new Paginator($queryBuilder);
 }
 /**
  * Apply limit and offset on data
  * @param int $offset
  * @param int $limit
  * @return static
  */
 public function limit($offset, $limit)
 {
     $this->criteria->setFirstResult($offset)->setMaxResults($limit);
     return $this;
 }
 public function testAddCriteriaLimit()
 {
     $qb = $this->_em->createQueryBuilder();
     $criteria = new Criteria();
     $criteria->setFirstResult(2);
     $criteria->setMaxResults(10);
     $qb->addCriteria($criteria);
     $this->assertEquals(2, $qb->getFirstResult());
     $this->assertEquals(10, $qb->getMaxResults());
 }
 /**
  * @param integer $offset
  *
  * @return mixed
  */
 public function offset($offset)
 {
     $this->criteria->setFirstResult($offset);
 }
Example #11
0
 /**
  * {@inheritDoc}
  */
 public function getItems($offset, $itemCountPerPage)
 {
     $this->criteria->setFirstResult($offset)->setMaxResults($itemCountPerPage);
     return $this->selectable->matching($this->criteria)->toArray();
 }
Example #12
0
 /**
  * @param int $firstResult
  * @return $this
  */
 public function setFirstResult($firstResult)
 {
     $this->criteria->setFirstResult($firstResult);
     return $this;
 }
Example #13
0
 public function testAddCriteriaLimit()
 {
     $qb = $this->_em->createQueryBuilder();
     $qb->select('u')->from('Doctrine\\Tests\\Models\\CMS\\CmsUser', 'u');
     $criteria = new Criteria();
     $criteria->setFirstResult(2);
     $criteria->setMaxResults(10);
     $qb->addCriteria($criteria);
     $this->assertEquals(2, $qb->getFirstResult());
     $this->assertEquals(10, $qb->getMaxResults());
 }