From a05cb7a1ab77c5e3d215f6fd580e39bc8aaef3fa Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 6 Jul 2016 15:12:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E7=9A=84getDat?= =?UTF-8?q?a=E5=92=8CgetAttr=E6=96=B9=E6=B3=95=20=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E5=B1=9E=E6=80=A7=E6=8A=9B=E5=87=BA=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=20=E4=BF=AE=E6=AD=A3Route=E7=B1=BB=E9=97=AD=E5=8C=85?= =?UTF-8?q?=E7=9A=84=E8=B7=AF=E7=94=B1=E5=8F=82=E6=95=B0=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lang/zh-cn.php | 1 + library/think/Model.php | 40 ++++++++++++++++++++++++---------------- library/think/Route.php | 2 ++ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lang/zh-cn.php b/lang/zh-cn.php index be26c53a..44731c34 100644 --- a/lang/zh-cn.php +++ b/lang/zh-cn.php @@ -25,6 +25,7 @@ return [ 'module not exists' => '模块不存在', 'controller not exists' => '控制器不存在', 'class not exists' => '类不存在', + 'property not exists' => '类的属性不存在', 'template not exists' => '模板文件不存在', 'illegal controller name' => '非法的控制器名称', 'illegal action name' => '非法的操作名称', diff --git a/library/think/Model.php b/library/think/Model.php index bae30c64..f8a06968 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -11,6 +11,7 @@ namespace think; +use InvalidArgumentException; use think\Cache; use think\Db; use think\db\Query; @@ -235,6 +236,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param string $name 字段名 留空获取全部 * @return mixed + * @throws InvalidArgumentException */ public function getData($name = null) { @@ -243,7 +245,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } elseif (array_key_exists($name, $this->data)) { return $this->data[$name]; } else { - return false; + throw new InvalidArgumentException('property not exists:' . __CLASS__ . '->' . $name); } } @@ -358,25 +360,31 @@ abstract class Model implements \JsonSerializable, \ArrayAccess * @access public * @param string $name 名称 * @return mixed + * @throws InvalidArgumentException */ public function getAttr($name) { - $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]); - } elseif (false === $value && method_exists($this, $name)) { - // 不存在该字段 获取关联数据 - $value = $this->relation()->getRelation($name); - // 保存关联对象值 - $this->data[$name] = $value; + 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; + } catch (InvalidArgumentException $e) { + if (method_exists($this, $name) && !method_exists('\think\Model', $name)) { + // 不存在该字段 获取关联数据 + $value = $this->relation()->getRelation($name); + // 保存关联对象值 + $this->data[$name] = $value; + } else { + throw new InvalidArgumentException('property not exists:' . __CLASS__ . '->' . $name); + } } - return $value; } /** diff --git a/library/think/Route.php b/library/think/Route.php index 8b24d8a7..14eaf67d 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -1004,6 +1004,8 @@ class Route } } if ($route instanceof \Closure) { + // 解析路由参数 + Request::instance()->param(array_merge($match, $_GET)); // 执行闭包 return ['type' => 'function', 'function' => $route, 'params' => $match]; }