PHP code example of lucasgiori / pagination-doctrine

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

    

lucasgiori / pagination-doctrine example snippets


namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class Task
 *
 * @ORM\Table(name="task")
 * @ORM\Entity(repositoryClass="DoctrinePagination\ORM\PaginatedRepository")
 */
class Task
{

}

namespace Repository;

use DoctrinePagination\ORM\PaginatedQueryBuilder;
use DoctrinePagination\ORM\PaginatedRepository;

/**
 * Class TaskRepository
 */
class TaskRepository extends PaginatedRepository
{

}

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Class Task
 *
 * @ORM\Table(name="task")
 * @ORM\Entity(repositoryClass="Repository\TaskRepository")
 */
class Task
{

}

protected function processCriteria(PaginatedQueryBuilder $qb, array $criteria)
{
    foreach ($criteria as $field => $value) {
        switch ($field) {
            case 'description':
                $qb->andWhere(...);
                unset($criteria[$field]);
                break;
        }
    }

    parent::processCriteria($qb, $criteria);
}

use Doctrine\ORM\Query;
use DoctrinePagination\DTO\Params;

$params = (new Params())
                ->setCriteria(["field" => "teste"]) // Array the fields and values to apply filter in sql
                ->setPage(1) // Page of query data
                ->setPerPage(10) // Quantity per page
                ->setHydrateMode(Query::HYDRATE_ARRAY) //Result handling mode
                ->setSearchField("nome") // Search Field define field to apply `like` of sql
                ->setSearch("gazin"); // Field Value  apply `like` in sql




use DoctrinePagination\ORM\PaginatedRepository;
use DoctrinePagination\DTO\Params;

class Example extends PaginatedRepository {


    public function findWithFilter(Params $params): ?PaginatedArrayCollection
    {
        return $this->findPageWithDTO($params);
    }
}

// some parameters
$page = 5;
$per_page = 10;

// get repository
$repository = $doctrine->getRepository('Task');

/** @var PaginatedArrayCollection */
$result = $repository->findPageBy($page, $per_page, ['field'=>'value']);

// count obtained results as usual
$pageResults = $result->count(); // 10

// get total results
$totalResults = $result->getTotal(); // 95

// current page
$currentPage = $result->getPage(); // 5

// current results per page
$currentResultsPerPage = $result->getResultsPerPage(); // 10

// get total pages
$totalPages = $result->getPages(); // 10

// get next page number
$nextPage = $result->getNextPage(); // 6

// get prev page number
$prevPage = $result->getPrevPage(); // 4