Skip to content

Commit 953309e

Browse files
authored
fix: prevent updateBatch with existing where conditions (#10236)
1 parent 5c0cbfe commit 953309e

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎system/Database/BaseBuilder.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2589,6 +2589,13 @@ protected function validateUpdate(): bool
25892589
*/
25902590
public function updateBatch($set = null, $constraints = null, int $batchSize = 100)
25912591
{
2592+
if ($this->QBWhere !== []) {
2593+
throw new DatabaseException(
2594+
'updateBatch() cannot be safely combined with existing Query Builder WHERE conditions. '
2595+
. 'Use updateBatch($data, $constraints), onConstraint(), or include all required constraint fields in the batch data.',
2596+
);
2597+
}
2598+
25922599
$this->onConstraint($constraints);
25932600

25942601
if (isset($this->QBOptions['setQueryAsData'])) {

‎tests/system/Database/Builder/UpdateTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ public function testUpdateBatchThrowsExceptionWithNoData(): void
299299
$builder->updateBatch(null, 'id');
300300
}
301301

302+
public function testUpdateBatchThrowsExceptionWithWhere(): void
303+
{
304+
$builder = new BaseBuilder('jobs', $this->db);
305+
306+
$this->expectException(DatabaseException::class);
307+
$this->expectExceptionMessage(
308+
'updateBatch() cannot be safely combined with existing Query Builder WHERE conditions. '
309+
. 'Use updateBatch($data, $constraints), onConstraint(), or include all required constraint fields in the batch data.',
310+
);
311+
312+
$builder->where('description', 'old')->updateBatch([
313+
[
314+
'id' => 2,
315+
'name' => 'Comedian',
316+
],
317+
], 'id');
318+
}
319+
302320
public function testUpdateBatchThrowsExceptionWithNoID(): void
303321
{
304322
$builder = new BaseBuilder('jobs', $this->db);

‎user_guide_src/source/changelogs/v4.7.4.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Deprecations
3030
Bugs Fixed
3131
**********
3232

33+
- **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.
34+
3335
See the repo's
3436
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
3537
for a complete list of bugs fixed.

0 commit comments

Comments
 (0)