lampager / lampager-doctrine2
Doctrine 2 的快速分页
v0.2.2
2021-06-24 12:03 UTC
Requires
- php: ^5.6 || ^7.0 || ^8.0
- doctrine/orm: ^2.4.5
- lampager/lampager: ^0.4
Requires (Dev)
- ext-json: *
- doctrine/cache: ^1.11
- friendsofphp/php-cs-fixer: ^3.0
- nilportugues/sql-query-formatter: ^1.2.2
- php-coveralls/php-coveralls: ^1.0
- phpunit/phpunit: ^6.4
This package is auto-updated.
Last update: 2024-09-15 10:58:39 UTC
README
Lampager for Doctrine 2
Doctrine 2 的快速分页
要求
- PHP:
^5.6 || ^7.0 || ^8.0
- doctrine/orm:
^2.4.5
- lampager/lampager:
^0.4
矩阵
安装
composer require lampager/lampager-doctrine2
用法
基础
实例化 QueryBuilder 创建分页器。
$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);
它将运行优化的 DQL。
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
然后你将得到
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
问题
可查找(双向)查询?
抱歉,您不能使用可查找模式,因为 Doctrine DQL 不支持 UNION ALL
语法。😢
关于元组比较呢?
Doctrine DQL 不支持元组比较语法!😢
类
注意:另请参阅lampager/lampager。
API
注意:另请参阅lampager/lampager。
Paginator::__construct()
Paginator::create()
创建一个新的分页器实例。
static Paginator create(\Doctrine\ORM\QueryBuilder $builder): static Paginator::__construct(\Doctrine\ORM\QueryBuilder $builder)
Paginator::setMapping()
Paginator::setMapping(string[] $mapping): $this
参数
(string[])
$mapping
一个关联数组,包含$columnNameOrCursorKey => $fetchedFieldName
。
Paginator::aggregated()
Paginator::aggregated(bool $aggregated = true): $this
声明应使用 HAVING
而不是 WHERE
进行聚合。
Paginator::setMaxResults()
是 \Lampager\Paginator::limit()
的别名。
Paginator::setMaxResults(int $limit): $this
Paginator::transform()
将 Lampager 查询转换为 Doctrine 查询。
Paginator::transform(\Lampager\Query $query): \Doctrine\ORM\Query
Paginator::build()
执行配置 + 转换。
Paginator::build(\Lampager\Contracts\Cursor|array $cursor = []): \Doctrine\ORM\Query
Paginator::paginate()
执行配置 + 转换 + 处理。
Paginator::paginate(\Lampager\Contracts\Cursor|array $cursor = []): \Lampager\PaginationResult
参数
(mixed)
$cursor
一个包含$column => $value
的关联数组,或者一个实现\Lampager\Contracts\Cursor
的对象。它必须是 全或无。- 对于第一页,省略此参数或传递空数组。
- 对于后续页面,传递所有参数。不允许部分参数。