Skip to content

Commit

Permalink
改进修改器会执行多次的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Oct 16, 2018
1 parent c8dc524 commit 1b15ff0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions library/think/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ public function save($data = [], $where = [], $sequence = null)

// 重新记录原始数据
$this->origin = $this->data;
$this->set = [];

return true;
}
Expand Down
15 changes: 12 additions & 3 deletions library/think/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ trait Attribute
*/
private $data = [];

/**
* 修改器执行记录
* @var array
*/
private $set = [];

/**
* 原始数据
* @var array
Expand Down Expand Up @@ -304,10 +310,14 @@ public function getChangedData()
* @param string $name 属性名
* @param mixed $value 属性值
* @param array $data 数据
* @return $this
* @return void
*/
public function setAttr($name, $value, $data = [])
{
if (isset($this->set[$name])) {
return;
}

if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
// 自动写入的时间戳字段
$value = $this->autoWriteTimestamp($name);
Expand All @@ -325,8 +335,7 @@ public function setAttr($name, $value, $data = [])

// 设置数据对象属性
$this->data[$name] = $value;

return $this;
$this->set[$name] = true;
}

/**
Expand Down
1 change: 0 additions & 1 deletion library/think/route/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

class Domain extends RuleGroup
{

/**
* 架构函数
* @access public
Expand Down

0 comments on commit 1b15ff0

Please sign in to comment.