From 2b95e829d7b836ddabc69c0c8eafdcd29f0d5d19 Mon Sep 17 00:00:00 2001 From: michalsn Date: Mon, 25 May 2026 08:02:12 +0200 Subject: [PATCH] fix: prevent updateBatch with existing where conditions --- system/Database/BaseBuilder.php | 7 +++++++ tests/system/Database/Builder/UpdateTest.php | 18 ++++++++++++++++++ user_guide_src/source/changelogs/v4.7.4.rst | 2 ++ 3 files changed, 27 insertions(+) diff --git a/system/Database/BaseBuilder.php b/system/Database/BaseBuilder.php index d891b3d61954..0a27b0b33abc 100644 --- a/system/Database/BaseBuilder.php +++ b/system/Database/BaseBuilder.php @@ -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'])) { diff --git a/tests/system/Database/Builder/UpdateTest.php b/tests/system/Database/Builder/UpdateTest.php index eb1cfc7d9c92..5bb93ad2a598 100644 --- a/tests/system/Database/Builder/UpdateTest.php +++ b/tests/system/Database/Builder/UpdateTest.php @@ -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); diff --git a/user_guide_src/source/changelogs/v4.7.4.rst b/user_guide_src/source/changelogs/v4.7.4.rst index c10b04436f8a..244823a000f7 100644 --- a/user_guide_src/source/changelogs/v4.7.4.rst +++ b/user_guide_src/source/changelogs/v4.7.4.rst @@ -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 `_ for a complete list of bugs fixed.