Reputation: 7233
i am unable to pass Doctrine Object to ORMPaginator function here is the snippet to my code
Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;..............
if (count($SearchResults) > 0)
{
$ORMPaginator = new ORMPaginator($SearchResults);
}
else
{
$ORMPaginator = new ORMPaginator($VouchersRepo->createQueryBuilder('vouchers'));
}
$Adapter = new DoctrineAdapter($ORMPaginator);
$Paginator = new Paginator($Adapter);
$Paginator->setDefaultItemCountPerPage(5);
$page = (int) $this->params()->fromQuery('page', 1);
$Paginator->setCurrentPageNumber($page);
clearly $ORMPaginator = new ORMPaginator('pass me DQL'); requires Query to generate Paginator. Now how to pass Doctrine object(non DQL object) to ORMPaginator i cant get through this?
Upvotes: 1
Views: 1266
Reputation: 9472
To achieve that you need to use ORM paginator and Zend paginator both
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as PageAdapter;
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
use Zend\Paginator\Paginator as ZendPaginator;
You should first initialize the query builder For example if category is your entity you can do it as follows
$repository = $this->entityManager->getRepository('put your entity class here');
$queryBuilder = $repository->createQueryBuilder('what ever entity you want');
$queryBuilder->select('what ever your want');
$q = $queryBuilder->getDql();
$query = $this->entityManager->createQuery($q);
$paginator = new ZendPaginator(new PageAdapter(new ORMPaginator($query)));
I remember I learnt this technique from Some SO posting but I couldn't find it while posting this solution
Upvotes: 1