Skip to content

Commit f4d17d2

Browse files
committed
refactor: extract setCreatedField() and setUpdatedField()
1 parent a2c7b3f commit f4d17d2

1 file changed

Lines changed: 37 additions & 25 deletions

File tree

system/BaseModel.php

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

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

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

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)