Skip to content

Commit e59b133

Browse files
committed
primary key validation in insertBatch
1 parent f8797f4 commit e59b133

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

system/Model.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ protected function doInsertBatch(?array $set = null, ?bool $escape = null, int $
373373
if (! isset($row[$this->primaryKey])) {
374374
throw DataException::forEmptyPrimaryKey('insertBatch');
375375
}
376+
377+
// Validate the primary key value
378+
$this->validateID($row[$this->primaryKey], false);
376379
}
377380
}
378381

tests/system/Models/InsertModelTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,30 @@ public function testInsertWithInvalidPrimaryKeyWhenAutoIncrementDisabled(
431431
$this->createModel(WithoutAutoIncrementModel::class)->insert($insert);
432432
}
433433

434+
/**
435+
* @param mixed $invalidKey
436+
* @param class-string $exception
437+
*/
438+
#[DataProvider('provideInvalidPrimaryKeyValues')]
439+
public function testInsertBatchWithInvalidPrimaryKeyWhenAutoIncrementDisabled($invalidKey, string $exception, string $exceptionMessage): void
440+
{
441+
$this->expectException($exception);
442+
$this->expectExceptionMessage($exceptionMessage);
443+
444+
$insertData = [
445+
[
446+
'key' => 'valid_key_1',
447+
'value' => 'value1',
448+
],
449+
[
450+
'key' => $invalidKey, // Invalid key in second row
451+
'value' => 'value2',
452+
],
453+
];
454+
455+
$this->createModel(WithoutAutoIncrementModel::class)->insertBatch($insertData);
456+
}
457+
434458
public static function provideInvalidPrimaryKeyValues(): iterable
435459
{
436460
return [

tests/system/Models/UpdateModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public function testUpdateThrowDatabaseExceptionWithoutWhereClause($id, string $
562562
// $useSoftDeletes = false
563563
$this->createModel(JobModel::class);
564564

565-
$this->model->update($id, ['name' => 'Foo Bar']); // @phpstan-ignore argument.type
565+
$this->model->update($id, ['name' => 'Foo Bar']);
566566
}
567567

568568
public static function provideUpdateThrowDatabaseExceptionWithoutWhereClause(): iterable

0 commit comments

Comments
 (0)