Skip to content

Commit

Permalink
修正事件观察者
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Mar 1, 2025
1 parent 9baada0 commit dad343e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
75 changes: 37 additions & 38 deletions src/model/concern/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
namespace think\model\concern;

use BackedEnum;
use Closure;
use Stringable;
use think\db\Express;
use think\db\Raw;
use think\helper\Str;
use think\model\Collection;
use think\model\contract\EnumTransform;
use think\model\contract\FieldTypeTransform;
use think\model\contract\Typeable;
use think\model\contract\Modelable as Model;
use think\model\contract\Typeable;
use think\model\type\Date;
use think\model\type\DateTime;
use think\model\type\Json;
Expand All @@ -47,9 +46,9 @@ private function initializeData(array | object $data, bool $fromSave = false)
$data = $this->parseData($data);
$schema = $this->getFields();
$mapping = $this->getOption('mapping');
$fields = array_keys(array_merge($schema,$mapping));
$fields = array_keys(array_merge($schema, $mapping));

// 实体模型赋值
// 模型赋值
foreach ($data as $name => $value) {
if (in_array($name, $this->getOption('disuse'))) {
// 废弃字段
Expand Down Expand Up @@ -80,7 +79,7 @@ private function initializeData(array | object $data, bool $fromSave = false)
$trueName = $this->getRealFieldName($name);
$type = $schema[$trueName] ?? 'string';
}

if ($this->isView() || $this->isVirtual() || in_array($trueName, $fields)) {
// 读取数据后进行类型转换
if (!$fromSave || !$this->hasSetAttr($trueName)) {
Expand All @@ -104,7 +103,7 @@ private function initializeData(array | object $data, bool $fromSave = false)
$this->setOption('get', []);
}
}

/**
* 获取主键名.
*
Expand Down Expand Up @@ -232,7 +231,7 @@ protected function readTransform($value, string | array $type)
} elseif ($model->getOption('enumReadName')) {
$method = $model->getOption('enumReadName');
$value = is_string($method) ? $value->$method() : $value->name;
}
}
} else {
// 对象类型
$value = new $type($value);
Expand All @@ -242,17 +241,17 @@ protected function readTransform($value, string | array $type)
};

return match ($type) {
'string' => (string) $value,
'int','integer' => (int) $value,
'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''),
'bool','boolean' => (bool) $value,
'array' => empty($value) ? [] : (is_array($value) ? $value : json_decode($value, true)),
'object' => empty($value) ? new \stdClass() : (is_string($value) ? json_decode($value) : json_decode(json_encode($value, JSON_FORCE_OBJECT))),
'json' => $typeTransform(Json::class, $value, $this),
'date' => $typeTransform(Date::class, $value, $this),
'datetime' => $typeTransform(DateTime::class, $value, $this),
'timestamp' => $typeTransform(DateTime::class, $value, $this),
default => $typeTransform($type, $value, $this),
'string' => (string) $value,
'int', 'integer' => (int) $value,
'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''),
'bool', 'boolean' => (bool) $value,
'array' => empty($value) ? [] : (is_array($value) ? $value : json_decode($value, true)),
'object' => empty($value) ? new \stdClass() : (is_string($value) ? json_decode($value) : json_decode(json_encode($value, JSON_FORCE_OBJECT))),
'json' => $typeTransform(Json::class, $value, $this),
'date' => $typeTransform(Date::class, $value, $this),
'datetime' => $typeTransform(DateTime::class, $value, $this),
'timestamp' => $typeTransform(DateTime::class, $value, $this),
default => $typeTransform($type, $value, $this),
};
}

Expand Down Expand Up @@ -296,17 +295,17 @@ protected function writeTransform($value, string | array $type)
};

return match ($type) {
'string' => (string) $value,
'int','integer' => (int) $value,
'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''),
'bool','boolean' => (bool) $value,
'object' => is_object($value) ? json_encode($value, JSON_FORCE_OBJECT) : $value,
'array' => json_encode((array) $value, JSON_UNESCAPED_UNICODE),
'json' => $typeTransform(Json::class, $value, $this),
'date' => $typeTransform(Date::class, $value, $this),
'datetime' => $typeTransform(DateTime::class, $value, $this),
'timestamp' => $typeTransform(DateTime::class, $value, $this),
default => $typeTransform($type, $value, $this),
'string' => (string) $value,
'int', 'integer' => (int) $value,
'float' => empty($param) ? (float) $value : (float) number_format($value, (int) $param, '.', ''),
'bool', 'boolean' => (bool) $value,
'object' => is_object($value) ? json_encode($value, JSON_FORCE_OBJECT) : $value,
'array' => json_encode((array) $value, JSON_UNESCAPED_UNICODE),
'json' => $typeTransform(Json::class, $value, $this),
'date' => $typeTransform(Date::class, $value, $this),
'datetime' => $typeTransform(DateTime::class, $value, $this),
'timestamp' => $typeTransform(DateTime::class, $value, $this),
default => $typeTransform($type, $value, $this),
};
}

Expand Down Expand Up @@ -467,8 +466,8 @@ public function jsonAssoc(bool $assoc = true)
*/
public function set(string $name, $value)
{
$name = $this->getMappingName($name);
$type = $this->getFields($name);
$name = $this->getMappingName($name);
$type = $this->getFields($name);

if (is_null($value) && is_subclass_of($type, Model::class)) {
// 关联数据为空 设置一个空模型
Expand Down Expand Up @@ -523,11 +522,11 @@ private function setWithAttr(string $name, $value, array $data = [])
$step = $value->getStep();
$origin = $this->getOrigin($name);
$real = match ($value->getType()) {
'+' => $origin + $step,
'-' => $origin - $step,
'*' => $origin * $step,
'/' => $origin / $step,
default => $origin,
'+' => $origin + $step,
'-' => $origin - $step,
'*' => $origin * $step,
'/' => $origin / $step,
default => $origin,
};
$this->setData($name, $real);
} elseif (is_scalar($value)) {
Expand All @@ -548,7 +547,7 @@ private function setWithAttr(string $name, $value, array $data = [])
*/
public function get(string $name, bool $attr = true)
{
$name = $this->getMappingName($name);
$name = $this->getMappingName($name);
if ($attr && $value = $this->getWeakData('get', $name)) {
// 已经输出的数据直接返回
return $value;
Expand Down Expand Up @@ -625,7 +624,7 @@ private function getWithAttr(string $name, $value, array $data = [])
public function getAttr(string $name)
{
return $this->get($name);
}
}

/**
* 设置数据对象的值 并进行类型自动转换
Expand Down
2 changes: 1 addition & 1 deletion src/model/concern/ModelEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function trigger(string $event): bool
try {
if ($obser) {
$reflect = new ReflectionClass($obser);
$callable = $reflect->newinstance();
$observer = $reflect->newinstance();
} else {
$observer = $this;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/orm/ModelFieldMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testCustomTypeMapping()
{
// 测试自定义类型转换和获取器
$item = FieldMappingModel::find(1);
$this->assertEquals('25岁', $item->user_age);
$this->assertEquals($item->age, $item->user_age);

// 测试自定义修改器
$item->user_age = '30岁';
Expand Down Expand Up @@ -103,13 +103,13 @@ class FieldMappingModel extends Model {
];

// 自定义获取器
public function getUserAgeAttr($value)
public function getAgeAttr($value)
{
return $value . '';
}

// 自定义修改器
public function setUserAgeAttr($value)
public function setAgeAttr($value)
{
return intval($value);
}
Expand Down

0 comments on commit dad343e

Please # to comment.