PHP code example of abb / paginator

1. Go to this page and download the library: Download abb/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/ */

    

abb / paginator example snippets




use Abb\Paginator\Adapter\DoctrineDbalAdapter;
use Abb\Paginator\Paginator;
use Doctrine\DBAL\DriverManager;

$connection = DriverManager::getConnection(/*...*/);

$qb = $connection->createQueryBuilder()
    ->select('p.*')
    ->from('posts', 'p')
    ->where('p.published = true');

$adapter = new DoctrineDbalAdapter($qb);
$pageSize = 5; // default 10
$paginator = new Paginator($adapter, $pageSize);
$page = 1;
$paginationResult = $paginator->paginate($page); // will return \Abb\Paginator\PaginationResult object



use Abb\Paginator\Adapter\DoctrineDbalAdapter;
use Abb\Paginator\Paginator;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Query\QueryBuilder;

$connection = DriverManager::getConnection(/*...*/);

$qb = $connection->createQueryBuilder()
    ->select('p.*')
    ->from('posts', 'p')
    ->where('p.published = true');

$countQbModifier = function (QueryBuilder $qb): QueryBuilder {
   return $qb->select('count(distinct p.id) AS cnt')
       ->setMaxResults(1);
};

$adapter = new DoctrineDbalAdapter($qb, $countQbModifier);
$paginator = new Paginator($adapter);
$paginationResult = $paginator->paginate(1);

use Abb\Paginator\Adapter\DoctrineDbalPlainSqlAdapter;
use Abb\Paginator\Paginator;
use Doctrine\DBAL\DriverManager;

$connection = DriverManager::getConnection(/*...*/);

$sql = 'SELECT * FROM posts p LEFT JOIN comments c ON p.id = c.post_id WHERE p.username = :username ORDER BY p.id';

// WITH queries (PostgreSQL) are also supported, e.g.
// $sql = 'WITH posts_with_comments AS (SELECT * FROM posts p LEFT JOIN comments c ON p.id = c.post_id)
//     SELECT * FROM posts_with_comments pwc WHERE pwc.username = :username ORDER BY pwc.id';

$adapter = new DoctrineDbalPlainSqlAdapter($connection, $sql, ['username' => 'John Doe']);
$paginator = new Paginator($adapter);
$paginationResult = $paginator->paginate(1);