Skip to content

Commit

Permalink
fix: Model::insertBatch() to non auto-increment table causes error
Browse files Browse the repository at this point in the history
CodeIgniter\Database\Exceptions\DataException : There is no primary key defined when trying to make insertBatch.
  • Loading branch information
kenjis committed Aug 1, 2023
1 parent 6cd704d commit a2c4749
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
26 changes: 21 additions & 5 deletions system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch

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

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

/**
* Ensures that only the fields that are allowed to be updated
* are in the data array.
* Ensures that only the fields that are allowed to be updated are
* in the data array.
*
* Used by insert(), insertBatch(), update(), and updateBatch() to protect
* against mass assignment vulnerabilities.
* Used by update() and updateBatch() to protect against mass assignment
* vulnerabilities.
*
* @param array $data Data
*
Expand All @@ -1251,6 +1251,22 @@ protected function doProtectFields(array $data): array
return $data;
}

/**
* Ensures that only the fields that are allowed to be inserted are in
* the data array.
*
* Used by insert() and insertBatch() to protect against mass assignment
* vulnerabilities.
*
* @param array $data Data
*
* @throws DataException
*/
protected function doProtectFieldsForInsert(array $data): array
{
return $this->doProtectFields($data);
}

/**
* Sets the date or current date if null value is passed.
*
Expand Down
35 changes: 35 additions & 0 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,41 @@ public function insert($data = null, bool $returnID = true)
return parent::insert($data, $returnID);
}

/**
* Ensures that only the fields that are allowed to be inserted are in
* the data array.
*
* Used by insert() and insertBatch() to protect against mass assignment
* vulnerabilities.
*
* @param array $data Data
*
* @throws DataException
*/
protected function doProtectFieldsForInsert(array $data): array
{
if (! $this->protectFields) {
return $data;
}

if (empty($this->allowedFields)) {
throw DataException::forInvalidAllowedFields(static::class);
}

foreach (array_keys($data) as $key) {
// Do not remove the non-auto-incrementing primary key data.
if ($this->useAutoIncrement === false && $key === $this->primaryKey) {
continue;
}

if (! in_array($key, $this->allowedFields, true)) {
unset($data[$key]);
}
}

return $data;
}

/**
* Updates a single record in the database. If an object is provided,
* it will attempt to convert it into an array.
Expand Down

0 comments on commit a2c4749

Please sign in to comment.