PHP code example of ardiakov / paginator
1. Go to this page and download the library: Download ardiakov/paginator library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ardiakov / paginator example snippets
class DoctrineDataProvider implements DataProviderInterface
{
/**
* @var QueryBuilder
*/
private $queryBuilder;
/**
* @var Page
*/
private $currentPage;
public function __construct(QueryBuilder $queryBuilder, Page $currentPage)
{
$this->queryBuilder = $queryBuilder;
$this->currentPage = $currentPage;
}
public function getData(): iterable
{
return $this->queryBuilder
->setFirstResult($this->currentPage->getOffset())
->setMaxResults($this->currentPage->getLimit())
->getQuery()
->getResult();
}
public function countItems(): int
{
$cloneQb = clone $this->queryBuilder;
$alias = $cloneQb->getRootAliases()[0];
return $cloneQb->select(sprintf('count(%s)', $alias))
->getQuery()
->getSingleScalarResult();
}
public function getCurrentPage(): Page
{
return $this->currentPage;
}
}