Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jun 24, 2024
1 parent d7a420b commit 855c4b1
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
protected $globalScope = [];

/**
* 数据递增递减.
* 数据字段值的变化.
*
* @var array
*/
protected $inc = [];
protected $change = [];

/**
* Db对象
Expand Down Expand Up @@ -560,7 +560,7 @@ public function isEmpty(): bool
public function inc(string $field, float $step = 1)
{
$this->setAttr($field, ['INC', $step]);
$this->inc[$field] = [$this->origin[$field], '+', $step];
$this->change[$field] = $this->origin[$field] + $step;
return $this;
}

Expand All @@ -575,7 +575,7 @@ public function inc(string $field, float $step = 1)
public function dec(string $field, float $step = 1)
{
$this->setAttr($field, ['DEC', $step]);
$this->inc[$field] = [$this->origin[$field], '-', $step];
$this->change[$field] = $this->origin[$field] - $step;
return $this;
}

Expand Down Expand Up @@ -611,12 +611,12 @@ public function save(array|object $data = [], string $sequence = null): bool
// 写入回调
$this->trigger('AfterWrite');

if (!empty($this->inc)) {
if (!empty($this->change)) {
// 处理递增递减数据
foreach ($this->inc as $field => $val) {
$this->data[$field] = '+' == $val[1] ? $val[0] + $val[2] : $val[0] - $val[2];
foreach ($this->change as $field => $val) {
$this->data[$field] = $val;
}
$this->inc = [];
$this->change = [];
}

// 重新记录原始数据
Expand Down

0 comments on commit 855c4b1

Please sign in to comment.