wiistriker/doctrine-cursor-iterator
Composer 安装命令:
composer require wiistriker/doctrine-cursor-iterator
包简介
Iterate through large datasets
README 文档
README
Iterate through large database results with easy
Installation
composer require wiistriker/doctrine-cursor-paginator
Usage
Both Doctrine ORM and DBAL query builders are supported:
Usage for ORM
Create query builder as usual. Dont forget about orderBy and maxResults.
$testEntityRepository = $this->entityManager->getRepository(TestEntity::class); $qb = $testEntityRepository->createQueryBuilder('t') ->orderBy('t.id', 'ASC') ->setMaxResults(100) ; /** @var DoctrineORMCursorPaginator<TestEntity> $cursorPaginator */ $cursorPaginator = new DoctrineORMCursorPaginator($qb); foreach ($cursorPaginator as $testEntity) { //... }
DoctrineORMCursorPaginator fetches only 100 records per query, so it never loads the whole dataset into memory at once and can efficiently iterate through even large datasets. See Memory and the EntityManager below for an important caveat about object hydration.
First sql:
SELECT ... FROM table ORDER BY id ASC LIMIT 100
Next:
SELECT ... FROM table WHERE id > {$id_from_last_record} ORDER BY id ASC LIMIT 100
You can also specify more order by fields
$testEntityRepository = $this->entityManager->getRepository(TestEntity::class); $qb = $testEntityRepository->createQueryBuilder('t') ->select('t.id', 't.createdAt') ->orderBy('t.createdAt', 'DESC') ->addOrderBy('t.id', 'DESC') ->setMaxResults(100) ; /** @var DoctrineORMCursorPaginator<TestEntity> $cursorPaginator */ $cursorPaginator = new DoctrineORMCursorPaginator($qb); foreach ($cursorPaginator as $testEntity) { //... }
You can change hydration mode
$cursorPaginator = new DoctrineORMCursorPaginator($qb, AbstractQuery::HYDRATE_ARRAY);
And even set query hints
$cursorPaginator = new DoctrineORMCursorPaginator( queryBuilder: $qb, queryHints: [ 'fetchMode' => [ TestEntity::class => [ 'field' => ClassMetadataInfo::FETCH_EAGER ] ] ] );
You wanna batch? Lets batch:
$cursorPaginator = new DoctrineORMCursorPaginator($qb); foreach ($cursorPaginator->batch() as $entities) { foreach ($entities as $testEntity) { $cnt++; } }
By default batch size equals to maxResults but you can also specify desired amount by yourself:
$myBatchSize = 1000; $cursorPaginator = new DoctrineORMCursorPaginator($qb); foreach ($cursorPaginator->batch($myBatchSize) as $entities) { }
Memory and the EntityManager
The paginator limits how many rows each query returns, but with the default object hydration
(HYDRATE_OBJECT) Doctrine keeps every hydrated entity in the EntityManager's identity map. Over a large
dataset that map keeps growing, so the per-query limit alone does not keep memory flat. When you iterate
over many entities, clear the EntityManager periodically (batching makes a natural place to do it):
foreach ($cursorPaginator->batch() as $entities) { foreach ($entities as $entity) { // ... process the entity } $entityManager->clear(); // detach processed entities and free memory }
Keep in mind that clear() detaches all managed entities: flush any pending changes before calling it,
and don't keep references to entities you still expect to be managed. If you don't need managed objects at all,
array hydration avoids the identity map entirely and sidesteps the issue:
$cursorPaginator = new DoctrineORMCursorPaginator($qb, AbstractQuery::HYDRATE_ARRAY);
Usage for DBAL
Just use DoctrineDBALCursorPaginator instead.
$queryBuilder = $this->connection->createQueryBuilder(); $queryBuilder ->select('id', 'name') ->from('test') ->orderBy('id', 'ASC') ->setMaxResults(100) ; $cursorPaginator = new DoctrineDBALCursorPaginator($queryBuilder); foreach ($cursorPaginator as $row) { }
DBAL exposes no public getter for the ORDER BY clause, so by default the order
is read from the query builder via reflection. If you prefer to avoid reflection
(or your DBAL version changes its internals), pass the order explicitly. It must
mirror the orderBy()/addOrderBy() calls on the query builder:
$queryBuilder ->select('id', 'name') ->from('test') ->orderBy('id', 'ASC') ->setMaxResults(100) ; $cursorPaginator = new DoctrineDBALCursorPaginator($queryBuilder, ['id' => 'ASC']);
wiistriker/doctrine-cursor-iterator 适用场景与选型建议
wiistriker/doctrine-cursor-iterator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「doctrine」 「deep-page-problem」 「cursor-based-pagination」 「seek method」 「keyset pagination」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wiistriker/doctrine-cursor-iterator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wiistriker/doctrine-cursor-iterator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wiistriker/doctrine-cursor-iterator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Make pimcore migration simple
Bundle Symfony DaplosBundle
Plugins for Tactician commands using Doctrine, like wrapping every command in a transaction
Use FileMaker through CWP as your backend database
Data migration mechanism for php using dbal do doctrine with pdo, schema and queryBuilder
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-23