kadudutra/doctrine-pagination

Doctrine pagination

v2.1.3 2021-01-03 20:43 UTC

This package is auto-updated.

Last update: 2024-10-29 06:12:16 UTC


README

Minimum PHP Version Latest Stable Version Latest Unstable Version License Total Downloads

This library provides a paginated repository and collection for Doctrine.

Installation

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

$ composer require kadudutra/doctrine-pagination

Configure Repository

Use it as Entity repository

Configure PaginatedRepository in your entity:

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

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

}

Create your custom Paginated repository

Create custom repository extending PaginatedRepository:

namespace Repository;

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

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

}

Configure your Entity:

namespace Entity;

use Doctrine\ORM\Mapping as ORM;

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

}

If needed, override processCriteria method in your custom repository to add some custom actions:

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);
}

Using Paginated Repository

public findPageBy ($page, $per_page, array $criteria = [], array $orderBy = null)

Returns a paginated collection of elements that matches criteria.

public countBy (array $criteria = [])

Returns the total number of elements that matches criteria.

protected createPaginatedQueryBuilder (array $criteria = [], $indexBy = null)

This method is used by findPageBy and countBy methods to create a QueryBuilder, and can be used in other repository custom methods.

processCriteria (protected)

This method is called from createPaginatedQueryBuilder to add criteria conditions.

This can be overridden to customize those criteria conditions.

findBy and findAll

PaginatedRepository overrides findBy and findAll default Doctrine Repository methods to provides code compatibility.

Using Paginated Collections

The PaginatedRepository always returns a PaginatedArrayCollection:

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

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

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

count()

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

getTotal()

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

getPage()

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

getResultsPerPage()

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

getPages()

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

getNextPage()

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

getPrevPage()

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