mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 07:32:48 +08:00
改进Model类的getAttr方法
This commit is contained in:
@@ -365,7 +365,12 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
public function getAttr($name)
|
public function getAttr($name)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
$notFound = false;
|
||||||
$value = $this->getData($name);
|
$value = $this->getData($name);
|
||||||
|
} catch (InvalidArgumentException $e) {
|
||||||
|
$notFound = true;
|
||||||
|
}
|
||||||
|
|
||||||
// 检测属性获取器
|
// 检测属性获取器
|
||||||
$method = 'get' . Loader::parseName($name, 1) . 'Attr';
|
$method = 'get' . Loader::parseName($name, 1) . 'Attr';
|
||||||
if (method_exists($this, $method)) {
|
if (method_exists($this, $method)) {
|
||||||
@@ -373,19 +378,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
|
|||||||
} elseif (isset($this->type[$name])) {
|
} elseif (isset($this->type[$name])) {
|
||||||
// 类型转换
|
// 类型转换
|
||||||
$value = $this->readTransform($value, $this->type[$name]);
|
$value = $this->readTransform($value, $this->type[$name]);
|
||||||
}
|
} elseif ($notFound) {
|
||||||
return $value;
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
if (method_exists($this, $name) && !method_exists('\think\Model', $name)) {
|
if (method_exists($this, $name) && !method_exists('\think\Model', $name)) {
|
||||||
// 不存在该字段 获取关联数据
|
// 不存在该字段 获取关联数据
|
||||||
$value = $this->relation()->getRelation($name);
|
$value = $this->relation()->getRelation($name);
|
||||||
// 保存关联对象值
|
// 保存关联对象值
|
||||||
$this->data[$name] = $value;
|
$this->data[$name] = $value;
|
||||||
return $value;
|
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidArgumentException('property not exists:' . $this->class . '->' . $name);
|
throw new InvalidArgumentException('property not exists:' . $this->class . '->' . $name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user