Skip to content

Commit

Permalink
改进模型数据写入
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jan 12, 2021
1 parent f0bba0b commit f48dc09
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ protected function updateData(): bool
return true;
}

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

if ($this->autoWriteTimestamp && $this->updateTime) {
// 自动写入更新时间
Expand Down Expand Up @@ -686,19 +686,19 @@ protected function insertData(string $sequence = null): bool
}

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

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

$db = $this->db();

$db->transaction(function () use ($sequence, $allowFields, $db) {
$db->transaction(function () use ($data, $sequence, $allowFields, $db) {
$result = $db->strict(false)
->field($allowFields)
->replace($this->replace)
->sequence($sequence)
->insert($this->data, true);
->insert($data, true);

// 获取自动增长主键
if ($result) {
Expand Down
6 changes: 4 additions & 2 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,9 @@ protected function readDataType(array &$data): void
* 写入数据类型处理
* @access protected
* @param array $data 数据
* @return void
* @return array
*/
protected function writeDataType(array &$data): void
protected function writeDataType(array $data): array
{
foreach ($data as $name => &$value) {
if (isset($this->type[$name])) {
Expand All @@ -542,6 +542,8 @@ protected function writeDataType(array &$data): void
$value = $this->autoWriteTimestamp();
}
}

return $data;
}

/**
Expand Down

0 comments on commit f48dc09

Please sign in to comment.