From e9b60d1e4d78b761554837500dbff855aa057b87 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sat, 4 Jun 2016 10:55:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB=E7=9A=84getDat?= =?UTF-8?q?a=E6=96=B9=E6=B3=95=E5=92=8CgetAttr=E6=96=B9=E6=B3=95=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0autoFetchFieldType=E5=B1=9E=E6=80=A7=20?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=AE=BE=E7=BD=AE=E6=98=AF=E5=90=A6=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E8=8E=B7=E5=8F=96=E5=AD=97=E6=AE=B5=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=20=E9=BB=98=E8=AE=A4=E4=B8=BAtrue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 50 ++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 77d4a3bf..b3eea262 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -63,6 +63,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $insert = []; // 更新自动完成列表 protected $update = []; + // 是否自动获取字段类型信息 + protected $autoFetchFieldType = true; // 是否需要自动写入时间戳 protected $autoWriteTimestamp; // 创建时间字段 @@ -108,15 +110,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->class = get_class($this); if (empty($this->name)) { + // 当前模型名 $this->name = basename(str_replace('\\', '/', $this->class)); } - if (empty($this->fieldType)) { - // 获取字段类型信息并缓存 + if ($this->autoFetchFieldType && empty($this->fieldType)) { + // 获取字段类型信息 $this->fieldType = $this->db()->getTableInfo('', 'type'); } if (is_null($this->autoWriteTimestamp)) { + // 自动写入时间戳 $this->autoWriteTimestamp = $this->db()->getConfig('auto_timestamp'); } @@ -178,6 +182,23 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $this; } + /** + * 获取对象原始数据 如果不存在指定字段返回false + * @access public + * @param string $name 字段名 留空获取全部 + * @return mixed + */ + public function getData($name = null) + { + if (is_null($name)) { + return $this->data; + } elseif (array_key_exists($name, $this->data)) { + return $this->data[$name]; + } else { + return false; + } + } + /** * 修改器 设置数据对象值 * @access public @@ -287,17 +308,17 @@ abstract class Model implements \JsonSerializable, \ArrayAccess */ public function getAttr($name) { - $value = isset($this->data[$name]) ? $this->data[$name] : null; + $value = $this->getData($name); // 检测属性获取器 $method = 'get' . Loader::parseName($name, 1) . 'Attr'; if (method_exists($this, $method)) { $value = $this->$method($value, $this->data); - } elseif (!is_null($value) && isset($this->type[$name])) { + } elseif (isset($this->type[$name])) { // 类型转换 $value = $this->readTransform($value, $this->type[$name]); - } elseif (is_null($value) && !isset($this->fieldType[$name]) && method_exists($this, $name)) { - // 获取关联数据 + } elseif (false === $value && method_exists($this, $name)) { + // 不存在该字段 获取关联数据 $value = $this->relation()->getRelation($name); // 保存关联对象值 $this->data[$name] = $value; @@ -350,17 +371,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess return $value; } - /** - * 获取对象原始数据 - * @access public - * @param string $name 字段名 留空获取全部 - * @return array - */ - public function getData($name = '') - { - return array_key_exists($name, $this->data) ? $this->data[$name] : $this->data; - } - /** * 设置需要追加的输出属性 * @access public @@ -374,7 +384,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } /** - * 设置需要隐藏的属性 + * 设置需要隐藏的输出属性 * @access public * @param array $hidden 属性列表 * @return $this @@ -662,9 +672,9 @@ abstract class Model implements \JsonSerializable, \ArrayAccess } /** - * 设置自动完成的字段 + * 设置自动完成的字段( 规则通过修改器定义) * @access public - * @param array $fields 需要自动完成的字段( 规则通过修改器定义) + * @param array $fields 需要自动完成的字段 * @return $this */ public function auto($fields)