From 792f1462616b0cc4809455451af6855553585dfa Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 13 May 2016 09:54:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Model=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3=E5=AD=97=E6=AE=B5=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 4c974ef5..01802092 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -76,6 +76,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess protected $updateWhere; // 当前执行的关联对象 protected $relation; + // 属性类型 + protected $fieldType = []; /** * 初始化过的模型. @@ -99,7 +101,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess if (empty($this->name)) { $this->name = basename(str_replace('\\', '/', get_class($this))); } - + // 获取字段类型信息并缓存 + $this->fieldType = self::db()->getTableInfo('', 'type'); $this->initialize(); $this->relation = new Relation($this); } @@ -880,6 +883,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $name = !empty($class->name) ? $class->name : basename(str_replace('\\', '/', $model)); self::$links[$model]->name($name); } + // 设置当前模型 确保查询返回模型对象 self::$links[$model]->model($model); // 返回当前数据库对象 @@ -930,6 +934,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $value = NOW_TIME; break; } + } elseif (isset($this->fieldType[$name]) && preg_match('/(datetime|timestamp)/is', $this->fieldType[$name])) { + $value = date($this->dateFormat, NOW_TIME); } else { $value = NOW_TIME; } @@ -965,7 +971,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess break; case 'timestamp': $format = !empty($param) ? $param : $this->dateFormat; - $value = date($format, is_numeric($valiue) ? $value : strtotime($value)); + $value = date($format, is_numeric($value) ? $value : strtotime($value)); break; case 'object': if (is_object($value)) {