From b3fb1c6045c6041972c10c04b14967331ced3c12 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 6 Jul 2016 17:22:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E7=9A=84getAtt?= =?UTF-8?q?r=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 9e6347fb..7d5c96c2 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -365,27 +365,30 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public function getAttr($name) { try { - $value = $this->getData($name); - // 检测属性获取器 - $method = 'get' . Loader::parseName($name, 1) . 'Attr'; - if (method_exists($this, $method)) { - $value = $this->$method($value, $this->data); - } elseif (isset($this->type[$name])) { - // 类型转换 - $value = $this->readTransform($value, $this->type[$name]); - } - return $value; + $notFound = false; + $value = $this->getData($name); } catch (InvalidArgumentException $e) { + $notFound = true; + } + + // 检测属性获取器 + $method = 'get' . Loader::parseName($name, 1) . 'Attr'; + if (method_exists($this, $method)) { + $value = $this->$method($value, $this->data); + } elseif (isset($this->type[$name])) { + // 类型转换 + $value = $this->readTransform($value, $this->type[$name]); + } elseif ($notFound) { if (method_exists($this, $name) && !method_exists('\think\Model', $name)) { // 不存在该字段 获取关联数据 $value = $this->relation()->getRelation($name); // 保存关联对象值 $this->data[$name] = $value; - return $value; } else { throw new InvalidArgumentException('property not exists:' . $this->class . '->' . $name); } } + return $value; } /**