public function testCountAfterSlice()
 {
     $dql = "SELECT u FROM Pagerfanta\\Tests\\Adapter\\DoctrineORM\\User u";
     $query = $this->entityManager->createQuery($dql);
     $adapter = new DoctrineORMAdapter($query);
     $adapter->getSlice(0, 1);
     $this->assertEquals(2, $adapter->getNbResults());
 }
 /**
  * @ApiDoc(description="List of photos with possibility filtering by tags.", resource=true)
  *
  * @Rest\QueryParam(name="page", requirements="\d+", default="1", description="Page number.")
  * @Rest\QueryParam(name="tags", requirements=".+", map=true, description="Tags for filtering.")
  * @Rest\View(template="@TestTaskPhotos/Api/getPhotos.html.twig")
  */
 public function getPhotosAction($page, array $tags = null)
 {
     $doctrine = $this->getDoctrine();
     $tags = (array) $tags;
     if ($tags) {
         $tags = $doctrine->getRepository('TestTaskTagsBundle:Tag')->findBy(array('title' => $tags));
     }
     $photoRepository = $doctrine->getRepository('TestTaskPhotosBundle:Photo');
     $qb = $photoRepository->getPhotosQb($tags);
     $innerAdapter = new DoctrineORMAdapter($qb, false);
     // wrap adapter for getting possibility load tags as primitives
     $adapter = new CallbackAdapter(function () use($innerAdapter) {
         return $innerAdapter->getNbResults();
     }, function ($offset, $length) use($innerAdapter, $photoRepository) {
         $results = $innerAdapter->getSlice($offset, $length);
         $photoRepository->attachTagsToPhotos(iterator_to_array($results));
         return $results;
     });
     $pagerfanta = (new Pagerfanta($adapter))->setMaxPerPage(10)->setCurrentPage($page ?: 1);
     return new PhotosCollection($pagerfanta);
 }
 public function testItShouldAcceptAQueryBuilder()
 {
     $queryBuilder = $this->entityManager->createQueryBuilder()->select('u')->from('Pagerfanta\\Tests\\Adapter\\DoctrineORM\\User', 'u');
     $adapter = new DoctrineORMAdapter($queryBuilder);
     $this->assertSame(2, $adapter->getNbResults());
     $slice = $adapter->getSlice(0, 10);
     $this->assertSame(2, count($slice));
     $users = array($this->user1, $this->user2);
     $userClass = 'Pagerfanta\\Tests\\Adapter\\DoctrineORM\\User';
     foreach ($users as $key => $user) {
         $this->assertInstanceOf($userClass, $slice[$key]);
         $this->assertSame($user->id, $slice[$key]->id);
     }
 }
    public function testCaseBasedQuery()
    {
        if (version_compare(\Doctrine\ORM\Version::VERSION, '2.2.0-DEV', '<')) {
            $this->markTestSkipped('Only recent orm version can test against this query.');
        }
        $dql = <<<DQL
            SELECT p,
              CASE
                WHEN p.name LIKE :keyword
                  AND p.biography LIKE :keyword
                THEN 0

                WHEN p.name LIKE :keyword
                THEN 1

                WHEN p.biography LIKE :keyword
                THEN 2

                ELSE 3
              END AS relevance
            FROM Pagerfanta\\Tests\\Adapter\\DoctrineORM\\Person p
            WHERE (
              p.name LIKE :keyword
              OR p.biography LIKE :keyword
            )
            GROUP BY p.id
            ORDER BY relevance ASC, p.id DESC
DQL;
        $query = $this->entityManager->createQuery($dql);
        $query->setParameter('keyword', '%Foo%');
        $adapter = new DoctrineORMAdapter($query);
        $this->assertEquals(1, $adapter->getNbResults());
        $items = $adapter->getSlice(0, 10);
        $this->assertEquals('Foo', $items[0][0]->name);
        $this->assertEquals(1, $items[0]['relevance']);
    }
 /**
  * @inheritdoc
  */
 public function getPartialResult(Pagination $pagination)
 {
     $pager = new DoctrineORMAdapter($this->queryBuilder);
     $pageSize = $pagination->getPageSize();
     return new Result($pager->getSlice($pagination->calculateOffset(), $pageSize)->getArrayCopy(), $pager->getNbResults(), $pagination->getPageNumber(), $pageSize);
 }