Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ abstract public function countAllResults(bool $reset = true, bool $test = false)
* @return void
*
* @throws DataException
* @throws InvalidArgumentException if $size is not a positive integer
*/
abstract public function chunk(int $size, Closure $userFunc);

Expand Down
7 changes: 6 additions & 1 deletion system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Exceptions\BadMethodCallException;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\Exceptions\ModelException;
use CodeIgniter\Validation\ValidationInterface;
use Config\Database;
Expand Down Expand Up @@ -533,10 +534,14 @@ public function countAllResults(bool $reset = true, bool $test = false)
*/
public function chunk(int $size, Closure $userFunc)
{
if ($size <= 0) {
throw new InvalidArgumentException('chunk() requires a positive integer for the $size argument.');
}

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

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

Expand Down
58 changes: 58 additions & 0 deletions tests/system/Models/MiscellaneousModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
namespace CodeIgniter\Models;

use CodeIgniter\Database\Exceptions\DataException;
use CodeIgniter\Events\Events;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\I18n\Time;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Models\EntityModel;
Expand All @@ -39,6 +41,62 @@ public function testChunk(): void
$this->assertSame(4, $rowCount);
}

public function testChunkThrowsOnZeroSize(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('chunk() requires a positive integer for the $size argument.');

$this->createModel(UserModel::class)->chunk(0, static function ($row): void {});
}

public function testChunkThrowsOnNegativeSize(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('chunk() requires a positive integer for the $size argument.');

$this->createModel(UserModel::class)->chunk(-1, static function ($row): void {});
}

public function testChunkEarlyExit(): void
{
$rowCount = 0;

$this->createModel(UserModel::class)->chunk(2, static function ($row) use (&$rowCount): bool {
$rowCount++;

return false;
});

$this->assertSame(1, $rowCount);
}

public function testChunkDoesNotRunExtraQuery(): void
{
$queryCount = 0;
$listener = static function () use (&$queryCount): void {
$queryCount++;
};

Events::on('DBQuery', $listener);
$this->createModel(UserModel::class)->chunk(4, static function ($row): void {});
Events::removeListener('DBQuery', $listener);

$this->assertSame(2, $queryCount);
}

public function testChunkEmptyTable(): void
{
$this->db->table('user')->truncate();

$rowCount = 0;

$this->createModel(UserModel::class)->chunk(2, static function ($row) use (&$rowCount): void {
$rowCount++;
});

$this->assertSame(0, $rowCount);
}

public function testCanCreateAndSaveEntityClasses(): void
{
$entity = $this->createModel(EntityModel::class)->where('name', 'Developer')->first();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Bugs Fixed
- **ContentSecurityPolicy:** Fixed a bug where custom CSP tags were not removed from generated HTML when CSP was disabled. The method now ensures that all custom CSP tags are removed from the generated HTML.
- **ContentSecurityPolicy:** Fixed a bug where ``generateNonces()`` produces corrupted JSON responses by replacing CSP nonce placeholders with unescaped double quotes. The method now automatically JSON-escapes nonce attributes when the response Content-Type is JSON.
- **Model:** Fixed a bug where ``BaseModel::updateBatch()`` threw an exception when ``updateOnlyChanged`` was ``true`` and the index field value did not change.
- **Model:** Fixed a bug where ``Model::chunk()`` ran an unnecessary extra database query at the end of iteration. ``chunk()`` now also throws ``InvalidArgumentException`` when called with a non-positive chunk size.
- **Session:** Fixed a bug in ``MemcachedHandler`` where the constructor incorrectly threw an exception when ``savePath`` was not empty.
- **Toolbar:** Fixed a bug where the standalone toolbar page loaded from ``?debugbar_time=...`` was not interactive.

Expand Down
Loading