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
7 changes: 7 additions & 0 deletions system/Database/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,13 @@ protected function validateUpdate(): bool
*/
public function updateBatch($set = null, $constraints = null, int $batchSize = 100)
{
if ($this->QBWhere !== []) {
throw new DatabaseException(
'updateBatch() cannot be safely combined with existing Query Builder WHERE conditions. '
. 'Use updateBatch($data, $constraints), onConstraint(), or include all required constraint fields in the batch data.',
);
}

$this->onConstraint($constraints);

if (isset($this->QBOptions['setQueryAsData'])) {
Expand Down
18 changes: 18 additions & 0 deletions tests/system/Database/Builder/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ public function testUpdateBatchThrowsExceptionWithNoData(): void
$builder->updateBatch(null, 'id');
}

public function testUpdateBatchThrowsExceptionWithWhere(): void
{
$builder = new BaseBuilder('jobs', $this->db);

$this->expectException(DatabaseException::class);
$this->expectExceptionMessage(
'updateBatch() cannot be safely combined with existing Query Builder WHERE conditions. '
. 'Use updateBatch($data, $constraints), onConstraint(), or include all required constraint fields in the batch data.',
);

$builder->where('description', 'old')->updateBatch([
[
'id' => 2,
'name' => 'Comedian',
],
], 'id');
}

public function testUpdateBatchThrowsExceptionWithNoID(): void
{
$builder = new BaseBuilder('jobs', $this->db);
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.7.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Deprecations
Bugs Fixed
**********

- **Database:** Fixed a bug where ``updateBatch()`` could be called after Query Builder ``where()`` conditions, even though it's not supported. In this situation, now the ``DatabaseException`` is thrown.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.
Loading