Skip to content

Commit 1b15ff0

Browse files
committed
改进修改器会执行多次的问题
1 parent c8dc524 commit 1b15ff0

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

library/think/Model.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ public function save($data = [], $where = [], $sequence = null)
439439

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

443444
return true;
444445
}

library/think/model/concern/Attribute.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ trait Attribute
7272
*/
7373
private $data = [];
7474

75+
/**
76+
* 修改器执行记录
77+
* @var array
78+
*/
79+
private $set = [];
80+
7581
/**
7682
* 原始数据
7783
* @var array
@@ -304,10 +310,14 @@ public function getChangedData()
304310
* @param string $name 属性名
305311
* @param mixed $value 属性值
306312
* @param array $data 数据
307-
* @return $this
313+
* @return void
308314
*/
309315
public function setAttr($name, $value, $data = [])
310316
{
317+
if (isset($this->set[$name])) {
318+
return;
319+
}
320+
311321
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
312322
// 自动写入的时间戳字段
313323
$value = $this->autoWriteTimestamp($name);
@@ -325,8 +335,7 @@ public function setAttr($name, $value, $data = [])
325335

326336
// 设置数据对象属性
327337
$this->data[$name] = $value;
328-
329-
return $this;
338+
$this->set[$name] = true;
330339
}
331340

332341
/**

library/think/route/Domain.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
class Domain extends RuleGroup
2222
{
23-
2423
/**
2524
* 架构函数
2625
* @access public

0 commit comments

Comments
 (0)