Skip to content

Commit e0f9d75

Browse files
committed
refactor: extract setCreatedField() and setUpdatedField()
1 parent 6b42bbf commit e0f9d75

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

system/BaseModel.php

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -784,14 +784,8 @@ public function insert($data = null, bool $returnID = true)
784784

785785
// Set created_at and updated_at with same time
786786
$date = $this->setDate();
787-
788-
if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $data)) {
789-
$data[$this->createdField] = $date;
790-
}
791-
792-
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $data)) {
793-
$data[$this->updatedField] = $date;
794-
}
787+
$data = $this->setCreatedField($data, $date);
788+
$data = $this->setUpdatedField($data, $date);
795789

796790
$eventData = ['data' => $data];
797791

@@ -823,6 +817,36 @@ public function insert($data = null, bool $returnID = true)
823817
return $returnID ? $this->insertID : $result;
824818
}
825819

820+
/**
821+
* Set datetime to created field.
822+
*
823+
* @phpstan-param row_array $row
824+
* @param int|string $date timestamp or datetime string
825+
*/
826+
protected function setCreatedField(array $row, $date): array
827+
{
828+
if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $row)) {
829+
$row[$this->createdField] = $date;
830+
}
831+
832+
return $row;
833+
}
834+
835+
/**
836+
* Set datetime to updated field.
837+
*
838+
* @phpstan-param row_array $row
839+
* @param int|string $date timestamp or datetime string
840+
*/
841+
protected function setUpdatedField(array $row, $date): array
842+
{
843+
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) {
844+
$row[$this->updatedField] = $date;
845+
}
846+
847+
return $row;
848+
}
849+
826850
/**
827851
* Compiles batch insert runs the queries, validating each row prior.
828852
*
@@ -872,14 +896,8 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
872896

873897
// Set created_at and updated_at with same time
874898
$date = $this->setDate();
875-
876-
if ($this->useTimestamps && $this->createdField !== '' && ! array_key_exists($this->createdField, $row)) {
877-
$row[$this->createdField] = $date;
878-
}
879-
880-
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) {
881-
$row[$this->updatedField] = $date;
882-
}
899+
$row = $this->setCreatedField($row, $date);
900+
$row = $this->setUpdatedField($row, $date);
883901
}
884902
}
885903

@@ -946,9 +964,7 @@ public function update($id = null, $data = null): bool
946964
throw DataException::forEmptyDataset('update');
947965
}
948966

949-
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $data)) {
950-
$data[$this->updatedField] = $this->setDate();
951-
}
967+
$data = $this->setUpdatedField($data, $this->setDate());
952968

953969
$eventData = [
954970
'id' => $id,
@@ -1032,9 +1048,7 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10321048
$row[$index] = $updateIndex;
10331049
}
10341050

1035-
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, $row)) {
1036-
$row[$this->updatedField] = $this->setDate();
1037-
}
1051+
$row = $this->setUpdatedField($row, $this->setDate());
10381052
}
10391053
}
10401054

@@ -1166,9 +1180,7 @@ public function replace(?array $data = null, bool $returnSQL = false)
11661180
return false;
11671181
}
11681182

1169-
if ($this->useTimestamps && $this->updatedField !== '' && ! array_key_exists($this->updatedField, (array) $data)) {
1170-
$data[$this->updatedField] = $this->setDate();
1171-
}
1183+
$data = $this->setUpdatedField((array) $data, $this->setDate());
11721184

11731185
return $this->doReplace($data, $returnSQL);
11741186
}

0 commit comments

Comments
 (0)