Skip to content

Commit

Permalink
Merge pull request codeigniter4#7760 from kenjis/refactor-model-inser…
Browse files Browse the repository at this point in the history
…t-noAutoIncrement

refactor: remove Model::$tempPrimaryKeyValue
  • Loading branch information
kenjis committed Aug 6, 2023
2 parents 76c7d7e + b9dd62d commit eaa8f1a
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 23 deletions.
2 changes: 1 addition & 1 deletion system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ public function insert($data = null, bool $returnID = true)

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

// doProtectFields() can further remove elements from
// $data so we need to check for empty dataset again
Expand Down
22 changes: 0 additions & 22 deletions system/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,6 @@ class Model extends BaseModel
*/
protected $escape = [];

/**
* Primary Key value when inserting and useAutoIncrement is false.
*
* @var int|string|null
*/
private $tempPrimaryKeyValue;

/**
* Builder method names that should not be used in the Model.
*
Expand Down Expand Up @@ -281,13 +274,6 @@ protected function doInsert(array $data)
$escape = $this->escape;
$this->escape = [];

// If $useAutoIncrement is false, add the primary key data.
if ($this->useAutoIncrement === false && $this->tempPrimaryKeyValue !== null) {
$data[$this->primaryKey] = $this->tempPrimaryKeyValue;

$this->tempPrimaryKeyValue = null;
}

// Require non-empty primaryKey when
// not using auto-increment feature
if (! $this->useAutoIncrement && empty($data[$this->primaryKey])) {
Expand Down Expand Up @@ -720,14 +706,6 @@ public function insert($data = null, bool $returnID = true)
}
}

if ($this->useAutoIncrement === false) {
if (is_array($data) && isset($data[$this->primaryKey])) {
$this->tempPrimaryKeyValue = $data[$this->primaryKey];
} elseif (is_object($data) && isset($data->{$this->primaryKey})) {
$this->tempPrimaryKeyValue = $data->{$this->primaryKey};
}
}

$this->escape = $this->tempData['escape'] ?? [];
$this->tempData = [];

Expand Down

0 comments on commit eaa8f1a

Please sign in to comment.