Skip to content

Commit 9813c04

Browse files
committed
数据保存后同步修改器或自动类型转换结果
1 parent 9c80ee5 commit 9813c04

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Entity.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ public function mapping(array $map)
662662
*/
663663
public function inc(string $field, float $step = 1)
664664
{
665-
$this->set($field, new Express('+',$step));
665+
$this->set($field, new Express('+', $step));
666666
return $this;
667667
}
668668

@@ -1299,6 +1299,24 @@ private function setWithAttr(string $name, $value, array $data = [])
12991299
// 类型转换
13001300
$value = $this->writeTransform($value, $this->getFields($name));
13011301
}
1302+
1303+
if ($value instanceof Express) {
1304+
// 处理运算表达式
1305+
$step = $value->getStep();
1306+
$origin = $this->getOrigin($name);
1307+
$real = match ($value->getType()) {
1308+
'+' => $origin + $step,
1309+
'-' => $origin - $step,
1310+
'*' => $origin * $step,
1311+
'/' => $origin / $step,
1312+
default => $origin,
1313+
};
1314+
$this->setData($name, $real);
1315+
} elseif (is_scalar($value)) {
1316+
// 同步写入修改器或类型自动转换结果
1317+
$this->setData($name, $value);
1318+
}
1319+
13021320
return $value;
13031321
}
13041322

src/db/Express.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public function __construct(protected string $type, protected float $step)
3030
{
3131
}
3232

33+
public function getStep()
34+
{
35+
return $this->step;
36+
}
37+
38+
public function getType()
39+
{
40+
return $this->type;
41+
}
42+
3343
/**
3444
* 获取表达式.
3545
*

0 commit comments

Comments
 (0)