Skip to content

Commit

Permalink
改进数据写入
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Aug 27, 2024
1 parent 7a41ff6 commit 444afc9
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ protected function updateData(): bool
// 检查允许字段
$allowFields = $this->checkAllowFields();

foreach ($this->relationWrite as $name => $val) {
foreach ($this->relationWrite as $val) {
if (!is_array($val)) {
continue;
}
Expand Down Expand Up @@ -760,34 +760,29 @@ protected function insertData(string $sequence = null): bool
}

$this->checkData();
$data = $this->data;

// 主键自动写入
if ($this->isAutoWriteId()) {
$pk = $this->getPk();
if (is_string($pk) && !isset($data[$pk])) {
$data[$pk] = $this->autoWriteId();
$this->data[$pk] = $data[$pk];
if (is_string($pk) && !isset($this->data[$pk])) {
$this->data[$pk] = $this->autoWriteId();
}
}

// 时间字段自动写入
if ($this->autoWriteTimestamp) {
if ($this->createTime && !array_key_exists($this->createTime, $data)) {
$data[$this->createTime] = $this->autoWriteTimestamp();
$this->data[$this->createTime] = $data[$this->createTime];
}

if ($this->updateTime && !array_key_exists($this->updateTime, $data)) {
$data[$this->updateTime] = $this->autoWriteTimestamp();
$this->data[$this->updateTime] = $data[$this->updateTime];
foreach ([$this->createTime, $this->updateTime] as $field) {
if ($field && !array_key_exists($field, $this->data)) {
$this->data[$field] = $this->autoWriteTimestamp();
}
}
}

// 检查允许字段
$allowFields = $this->checkAllowFields();

$db = $this->db();
$db = $this->db();
$data = $this->data;

$db->transaction(function () use ($data, $sequence, $allowFields, $db) {
$result = $db->strict(false)
Expand Down

0 comments on commit 444afc9

Please sign in to comment.