silarhi/cursor-pagination

A doctrine ORM cursor based pagination library for faster batch operations.

1.2.0 2024-06-05 16:52 UTC

This package is auto-updated.

Last update: 2024-11-18 10:14:36 UTC


README

Installation

composer require silarhi/cursor-pagination

Usage

use Silarhi\CursorPagination\Pagination\CursorPagination;

$queryBuilder = $entityManager
    ->createQueryBuilder('u')
    ->from(User::class, 'u')
    ->where('u.foo = true');

$configurations = new OrderConfigurations(
    new OrderConfiguration('u.createdAt', fn (User $user) => $user->getCreatedAt()),
    new OrderConfiguration('u.id', fn (User $user) => $user->getId()),
);

/** @var CursorPagination<User> $pagination */
$pagination = new CursorPagination($queryBuilder, $configurations, 100);

// Method 1: get results as chunk (recommended)
foreach($pagination->getChunkResults() as $results) {
    foreach($results as $user) {
        // do something with user
    }
    
    $entityManager->flush();
    $entityManager->clear();
}

// Method 2: get single result as iterator
foreach($pagination->getResults() as $user) {
    // do something with user
}

``