Skip to content

Commit a2c4749

Browse files
committed
fix: Model::insertBatch() to non auto-increment table causes error
CodeIgniter\Database\Exceptions\DataException : There is no primary key defined when trying to make insertBatch.
1 parent 6cd704d commit a2c4749

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

system/BaseModel.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
847847

848848
// Must be called first so we don't
849849
// strip out created_at values.
850-
$row = $this->doProtectFields($row);
850+
$row = $this->doProtectFieldsForInsert($row);
851851

852852
// Set created_at and updated_at with same time
853853
$date = $this->setDate();
@@ -1222,11 +1222,11 @@ public function protect(bool $protect = true)
12221222
}
12231223

12241224
/**
1225-
* Ensures that only the fields that are allowed to be updated
1226-
* are in the data array.
1225+
* Ensures that only the fields that are allowed to be updated are
1226+
* in the data array.
12271227
*
1228-
* Used by insert(), insertBatch(), update(), and updateBatch() to protect
1229-
* against mass assignment vulnerabilities.
1228+
* Used by update() and updateBatch() to protect against mass assignment
1229+
* vulnerabilities.
12301230
*
12311231
* @param array $data Data
12321232
*
@@ -1251,6 +1251,22 @@ protected function doProtectFields(array $data): array
12511251
return $data;
12521252
}
12531253

1254+
/**
1255+
* Ensures that only the fields that are allowed to be inserted are in
1256+
* the data array.
1257+
*
1258+
* Used by insert() and insertBatch() to protect against mass assignment
1259+
* vulnerabilities.
1260+
*
1261+
* @param array $data Data
1262+
*
1263+
* @throws DataException
1264+
*/
1265+
protected function doProtectFieldsForInsert(array $data): array
1266+
{
1267+
return $this->doProtectFields($data);
1268+
}
1269+
12541270
/**
12551271
* Sets the date or current date if null value is passed.
12561272
*

system/Model.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,41 @@ public function insert($data = null, bool $returnID = true)
730730
return parent::insert($data, $returnID);
731731
}
732732

733+
/**
734+
* Ensures that only the fields that are allowed to be inserted are in
735+
* the data array.
736+
*
737+
* Used by insert() and insertBatch() to protect against mass assignment
738+
* vulnerabilities.
739+
*
740+
* @param array $data Data
741+
*
742+
* @throws DataException
743+
*/
744+
protected function doProtectFieldsForInsert(array $data): array
745+
{
746+
if (! $this->protectFields) {
747+
return $data;
748+
}
749+
750+
if (empty($this->allowedFields)) {
751+
throw DataException::forInvalidAllowedFields(static::class);
752+
}
753+
754+
foreach (array_keys($data) as $key) {
755+
// Do not remove the non-auto-incrementing primary key data.
756+
if ($this->useAutoIncrement === false && $key === $this->primaryKey) {
757+
continue;
758+
}
759+
760+
if (! in_array($key, $this->allowedFields, true)) {
761+
unset($data[$key]);
762+
}
763+
}
764+
765+
return $data;
766+
}
767+
733768
/**
734769
* Updates a single record in the database. If an object is provided,
735770
* it will attempt to convert it into an array.

0 commit comments

Comments
 (0)