Skip to content

feat: Chunk array method in models#9962

Draft
patel-vansh wants to merge 5 commits intocodeigniter4:4.8from
patel-vansh:feat/chunk-array
Draft

feat: Chunk array method in models#9962
patel-vansh wants to merge 5 commits intocodeigniter4:4.8from
patel-vansh:feat/chunk-array

Conversation

@patel-vansh
Copy link
Contributor

Description
This PR adds a chunkArray() method in CodeIgniter\Model class which allows users to get chunk as a whole instead of single rows, so that they can easily use functions like array_column(...) inside the closure.

Related to this forum thread.

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

@patel-vansh
Copy link
Contributor Author

I guess this PR can only be merged after merging the devlop branch in 4.8, for that time trait linting.

@michalsn
Copy link
Member

You can rebase, although I would wait for changes from #9961.

Two things:

  1. I would change the name to chunkRows() since “array” in the name may be misleading, even though it technically describes the type passed to the callback.
  2. Since both methods that handle chunk results are nearly identical, I would abstract the shared logic, maybe like this:
private function iterateChunks(int $size): Generator
{
    if ($size <= 0) {
        throw new InvalidArgumentException('$size must be a positive integer.');
    }

    $total  = $this->builder()->countAllResults(false);
    $offset = 0;

    while ($offset < $total) {
        $builder = clone $this->builder();
        $rows    = $builder->get($size, $offset);

        if (! $rows) {
            throw DataException::forEmptyDataset('chunk');
        }

        $rows = $rows->getResult($this->tempReturnType);
        $offset += $size;

        if ($rows === []) {
            continue;
        }

        yield $rows;
    }
}

public function chunk(int $size, Closure $userFunc)
{
    foreach ($this->iterateChunks($size) as $rows) {
        foreach ($rows as $row) {
            if ($userFunc($row) === false) {
                return;
            }
        }
    }
}

public function chunkRows(int $size, Closure $userFunc)
{
    foreach ($this->iterateChunks($size) as $rows) {
        if ($userFunc($rows) === false) {
            return;
        }
    }
}

@michalsn michalsn added enhancement PRs that improve existing functionalities 4.8 PRs that target the `4.8` branch. labels Feb 19, 2026
@patel-vansh
Copy link
Contributor Author

I guess you're right. I will wait for #9961 to merge and then create a common method for both. Thanks for review @michalsn. :)

@patel-vansh patel-vansh marked this pull request as draft February 20, 2026 04:12
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. enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments