In your case it's not pagerfanta that does the sub-queries. It's the source your query builder instance it's coming from.
I usually have a function in entity repository that returns a plain query builder instance instead of the results. It's left to you to write an efficient query builder. Then I feed that query builder into DoctrineORMAdapter.
I've got this helper function that I use throughout my projects:
/**
* Pass an array, entity or a custom QueryBuilder instance to paginate.
* Takes an array of parameters as a second argument.
* Default parameter values:
*
* $params = array(
* 'curPage' => 1,
* 'perPage' => 15,
* 'order' => 'DESC'
* );
*
* @param mixed $object
* @param array $params
*
* @return Pagerfanta
*/
public function paginate($object, $params = array())
{
if (is_array($object)) {
$adapter = new ArrayAdapter($object);
} elseif ($this->isEntity($object)) {
$qb = $this->em->createQueryBuilder()
->select('s')
->from($this->getEntityName($object), 's')
->orderBy('s.id', isset($params['order']) ? $params['order'] : 'DESC');
$adapter = new DoctrineORMAdapter($qb);
} elseif ($object instanceof QueryBuilder) {
$adapter = new DoctrineORMAdapter($object);
}
$pager = new Pagerfanta($adapter);
$pager->setMaxPerPage(isset($params['perPage']) ? $params['perPage'] : 15);
$pager->setCurrentPage(isset($params['curPage']) ? $params['curPage'] : 1);
return $pager;
}
You can pass an array, entity or a query builder instance and it will return an appropriately paginated object ready to use.
You probably know how it's done, but anyway, here's what I have in my entity repository - one function returns query builder instance (perfect for pagerfanta), the other returns an array to be used elsewhere:
public function getMessageQueryBuilder($campaignId, $eqCriteriaArray = array(), $neqCriteriaArray = array())
{
$qb = $this->createQueryBuilder('m');
$qb->select('m')
->leftJoin('m.campaign', 'c')
->leftJoin('m.sentBy', 'u')
->where($qb->expr()->eq('m.campaign', $campaignId));
foreach ($eqCriteriaArray as $property => $value) {
$qb->andWhere($qb->expr()->eq($property, $qb->expr()->literal($value)));
}
foreach ($neqCriteriaArray as $property => $value) {
$qb->andWhere($qb->expr()->neq($property, $qb->expr()->literal($value)));
}
return $qb->orderBy('m.id', 'DESC');
}
public function filterMessages($campaignId, $eqCriteriaArray = array(), $neqCriteriaArray = array())
{
return $this->getMessageQueryBuilder($campaignId, $eqCriteriaArray, $neqCriteriaArray)->getQuery()->getResult();
Then I combine those two to get the actual pager object:
$singleSmsPager = $this->pagerUtil->paginate(
$this->em->getRepository('TreasureForgeMessageBundle:Message')
->getMessageQueryBuilder(CcToolSender::CAMPAIGN_ID, array(), array('u.username' => 'admin')),
array(
'curPage' => $singleSmsPage,
'perPage' => 10
)
);