1

我无法将 Doctrine Object 传递给 ORMPaginator 函数这是我的代码的片段

  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);

显然 $ORMPaginator = new ORMPaginator('pass me DQL'); 需要 Query 来生成 Paginator。现在如何将 Doctrine 对象(非 DQL 对象)传递给我无法通过的 ORMPaginator?

4

1 回答 1

1

为此,您需要同时使用 ORM 分页器和 Zend 分页器

use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as PageAdapter;
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
use Zend\Paginator\Paginator as ZendPaginator;

您应该首先初始化查询构建器例如,如果类别是您的实体,您可以按如下方式进行

  $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)));

我记得我从 Some SO 张贴中学到了这种技术,但是在发布这个解决方案时我找不到它

于 2013-09-20T16:36:11.700 回答