我已经添加了paginator.phtml
文件,并且在控制器中添加了一些代码
$adapter = new DoctrineAdapter(new ORMPaginator( $logList , false));
$paginator = new Paginator($adapter);
$paginator->setDefaultItemCountPerPage(10);
$paginator->setCurrentPageNumber(1);
return new ViewModel(['logList'=>$logList ,'form'=>$form,'posts' => $paginator]);
在查看页面上,我正在使用以下代码:
$this->paginationControl($posts,
'Sliding',
'ripple-site/partial/paginator',
['route' => 'ripplesite']
);
此代码产生错误:
传递给Doctrine \ ORM \ Tools \ Pagination \ Paginator :: cloneQuery()的参数1必须是Doctrine \ ORM \ Query的实例,给定数组,在C:\ zend \ vendor \ doctrine \ orm \ lib \中调用244行上的Doctrine \ ORM \ Tools \ Pagination \ Paginator.php
答案 0 :(得分:1)
现在做了几次,所以我将为您提供基本的indexAction
。 (请注意,OrmPaginator
中加载的内容有所不同,这可能是您遇到的问题)
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator as OrmPaginator;
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as OrmAdapter;
// class
public function indexAction()
{
$page = $this->params()->fromQuery('page', 1); // default page
$pageSize = $this->params()->fromQuery('pageSize', 10); // default page size
$orderBy = $this->params()->fromQuery('orderBy', 'createdAt'); // default order by (replace with your own property name!)
$orderDirection = $this->params()->fromQuery('orderDirection') === Criteria::ASC // default order direction (desc to display newest first - replace with your own)
? Criteria::ASC
: Criteria::DESC;
$criteria = (new Criteria())
->setFirstResult($page * $pageSize)
->setMaxResults($pageSize)
->orderBy([$orderBy => $orderDirection]);
/** @var QueryBuilder $qb */
$qb = $this->getObjectManager()->createQueryBuilder();
$qb->select('a') // replace with your own alias key
->from(Article::class, 'a') // replace with your own class and alias
->addCriteria($criteria);
$paginator = new Paginator(new OrmAdapter(new OrmPaginator($qb)));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($pageSize);
return [
'paginator' => $paginator,
'queryParams' => $this->params()->fromQuery(),
];
}
要求该类具有ObjectManager
的实例。如果您使用$entityManager
,那很好,只需将$this->getObjectManager()
替换为$this->getEntityManager()
或$this->entityManager
。
其余的应该开箱即用。
对于此控制器->操作,在index.phtml
中显示为
<?php
/** @var \Article\Entity\Article[] $paginator */
$pagination = $this->paginationControl(
$paginator,
'sliding',
'theme/partials/pagination', // This here assumes you have a partial setup for pagination. If not, leave a comment and I can add a default Bootstrap 4 compatible one.
[
'route' => 'admin/articles',
'queryParams' => $queryParams,
]
);
<?php if (isset($paginator) && $paginator->count() > 0) : ?>
<table class="table table-striped">
<thead>
<tr>
<th><?= $this->translate('Title') ?></th>
<!-- more columns -->
</tr>
</thead>
<tbody>
<?php foreach ($paginator as $article): ?>
<tr>
<td>
<?= $this->escapeHtml($article->getTitle()) ?>
</td>
<!-- more columns -->
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php if ($paginator->count() > 1) : ?>
<?= $pagination ?>
<?php endif ?>
<?php else : ?>
<p><?= $this->translate('No records found. Try again later or report an issue.') ?></p>
<?php endif ?>