PHP code example of lampager / lampager-doctrine2
1. Go to this page and download the library: Download lampager/lampager-doctrine2 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/ */
lampager / lampager-doctrine2 example snippets
$cursor = [
'p.id' => 3,
'p.createdAt' => '2017-01-10 00:00:00',
'p.updatedAt' => '2017-01-20 00:00:00',
];
$result = Paginator::create(
$entityManager
->getRepository(Post::class)
->createQueryBuilder('p')
->where('p.userId = :userId')
->setParameter('userId', 1)
)
->forward()
->setMaxResults(5) // Or ->limit(5)
->orderByDesc('p.updatedAt') // ORDER BY p.updatedAt DESC, p.createdAt DESC, p.id DESC
->orderByDesc('p.createdAt')
->orderByDesc('p.id')
->paginate($cursor);
object(Lampager\PaginationResult)#X (5) {
["records"]=>
array(5) {
[0]=>
object(App\Entities\Post)#X (5) {
["id"]=>
int(3)
["userId"]=>
int(1)
["text"]=>
string(3) "foo"
["createdAt"]=>
object(DateTimeImmutable)#X (3) {
["date"]=>
string(26) "2017-01-10 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["updatedAt"]=>
object(DateTimeImmutable)#X (3) {
["date"]=>
string(26) "2017-01-20 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
}
[1]=> ...
[2]=> ...
[3]=> ...
[4]=> ...
}
["hasPrevious"]=>
bool(false)
["previousCursor"]=>
NULL
["hasNext"]=>
bool(true)
["nextCursor"]=>
array(2) {
["p.updatedAt"]=>
object(DateTimeImmutable)#X (3) {
["date"]=>
string(26) "2017-01-18 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["p.createdAt"]=>
object(DateTimeImmutable)#X (3) {
["date"]=>
string(26) "2017-01-14 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
["id"]=>
int(6)
}
}
$result = Paginator::create(
$entityManager
->getRepository(Post::class)
->createQueryBuilder('p')
->select('p.id as postId, p.userId as authorUserId, p.createdAt, p.updatedAt') // Aliasing
->where('p.userId = :userId')
->setParameter('userId', 1)
)
->forward()
->setMaxResults(5)
->orderByDesc('p.updatedAt')
->orderByDesc('p.createdAt')
->orderByDesc('p.id')
->setMapping([
'p.id' => 'postId',
'p.userId' => 'authorUserId',
]) // Mapping
->paginate($cursor, Query::HYDRATE_ARRAY); // Hydration Mode
static Paginator create(\Doctrine\ORM\QueryBuilder $builder): static
Paginator::__construct(\Doctrine\ORM\QueryBuilder $builder)
Paginator::setMapping(string[] $mapping): $this
Paginator::aggregated(bool $aggregated = true): $this
Paginator::setMaxResults(int $limit): $this
Paginator::transform(\Lampager\Query $query): \Doctrine\ORM\Query
Paginator::build(\Lampager\Contracts\Cursor|array $cursor = []): \Doctrine\ORM\Query
Paginator::paginate(\Lampager\Contracts\Cursor|array $cursor = []): \Lampager\PaginationResult
sql
SELECT * FROM App\Entities\Post p
WHERE p.userId = 1
AND (
p.updatedAt = '2017-01-20 00:00:00' AND p.createdAt = '2017-01-10 00:00:00' AND p.id <= 3
OR
p.updatedAt = '2017-01-20 00:00:00' AND p.createdAt < '2017-01-10 00:00:00'
OR
p.updatedAt < '2017-01-20 00:00:00'
)
ORDER BY p.updatedAt DESC, p.createdAt DESC, p.id DESC
LIMIT 6