4
votes

I'm dealing with a fairly complex querybuilder (in a repository function), which is using a lot of things like partial c.{id,name,created} and a bunch of fetchjoins to try and keep both the amount of data and the amount of queries down.

This is however not enough, but the front end representation is paged anyway, so I'd like to make some ajax calls to fetch data when needed.

$qb = $this->createQueryBuilder('c')
  ->select('a whole bunch')
  ->join('many joins')
  ->setFirstResult(0)
  ->setMaxResults(10)

The above method doesn't work for complex joins, I do setMaxResults(10) and get 2 results, and setMaxResults(1000) gets me 118 results. Doctrine advises to use their Pagination class, as it will handle the count/iteration properly.

Now that works all fine if I loop over the Iterator object provided by new Paginator($query, true), but the code calling the repository function expects to get an Array from getArrayResult.

The iterator contains full entity objects, which means I would have to rewrite all the services to use methods instead of array keys.

Is there a way to use the Paginator with an ArrayResult?

1

1 Answers

10
votes

Add Hydration Mode to your query.

//Set query hydration mode
$query->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);