Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

How to add paging in Doctrine Zend Framework 3

I've already added paginator.phtml file and I have added some code in controller

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

On the view page I'm using this code:

$this->paginationControl($posts,
  'Sliding',
  'ripple-site/partial/paginator', 
  ['route' => 'ripplesite']
);

This code produces error:

Argument 1 passed to DoctrineORMToolsPaginationPaginator::cloneQuery() must be an instance of DoctrineORMQuery, array given, called in C:zendvendordoctrineormlibDoctrineORMToolsPaginationPaginator.php on line 244

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Done this a few times now, so I'll provide you with a basic indexAction. (Note the difference in what's loaded into the OrmPaginator, which is probably your issue)

use DoctrineCommonCollectionsCriteria;
use DoctrineORMQueryBuilder;
use DoctrineORMToolsPaginationPaginator as OrmPaginator;
use DoctrineORMModulePaginatorAdapterDoctrinePaginator 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(),
    ];
}

Requirements are that the class has the an instance of ObjectManager. If you use $entityManager, that's fine, just replace $this->getObjectManager() with $this->getEntityManager() or $this->entityManager.

The rest should work out of the box.


Display like so in index.phtml for this controller->action

<?php
/** @var ArticleEntityArticle[] $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 ?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...