Skip to content

feat: Add ID-based model chunking#10363

Open
memleakd wants to merge 2 commits into
codeigniter4:4.8from
memleakd:feat/model-chunk-by-id
Open

feat: Add ID-based model chunking#10363
memleakd wants to merge 2 commits into
codeigniter4:4.8from
memleakd:feat/model-chunk-by-id

Conversation

@memleakd

@memleakd memleakd commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds chunkById() and chunkRowsById() to CodeIgniter\Model.

This builds on #9962, which added chunkRows() for handling whole chunks at once. The existing chunk() and chunkRows() methods use offset-based pagination. This works fine for read-only processing, but it may skip rows when records are updated or deleted while the chunk loop is running.

The new methods process records ordered by the model primary key instead:

$userModel->where('country', 'US')
    ->chunkById(100, static function ($user) {
        // ...
    });

For cases where the whole chunk should be handled at once:

$userModel->where('country', 'US')
    ->chunkRowsById(100, static function ($users) {
        // ...
    });

A few restrictions are added intentionally. Since these methods manage their own primary key cursor and ordering, they cannot be used with orderBy(), groupBy(), limit(), offset(), or union(). The model also needs a configured primary key, and the primary key must be selected when custom select clauses are used.

This should be useful for jobs, exports, cleanup commands, and other long-running processing where rows may change during iteration.

Tests cover normal processing, early exit, soft deletes, deleting rows while processing, invalid chunk sizes, missing primary key cases, and incompatible builder state.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Jul 1, 2026
@memleakd memleakd force-pushed the feat/model-chunk-by-id branch from 8d27f76 to 761bedd Compare July 1, 2026 08:37
- Add chunkById() and chunkRowsById() for primary-key ordered chunking
- Reject incompatible builder state like orderBy(), groupBy(), limit(), offset(), and union()
- Document ID-based chunking behavior and constraints
- Add live model coverage for stable deletion, soft deletes, early exit, and invalid query shapes

Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@memleakd memleakd force-pushed the feat/model-chunk-by-id branch from 761bedd to 64042b0 Compare July 1, 2026 08:42
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant