修正Model 增加时间戳字段自动识别

This commit is contained in:
thinkphp
2016-05-13 09:54:26 +08:00
parent c3af0bb1c4
commit 792f146261

View File

@@ -76,6 +76,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $updateWhere; protected $updateWhere;
// 当前执行的关联对象 // 当前执行的关联对象
protected $relation; protected $relation;
// 属性类型
protected $fieldType = [];
/** /**
* 初始化过的模型. * 初始化过的模型.
@@ -99,7 +101,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
if (empty($this->name)) { if (empty($this->name)) {
$this->name = basename(str_replace('\\', '/', get_class($this))); $this->name = basename(str_replace('\\', '/', get_class($this)));
} }
// 获取字段类型信息并缓存
$this->fieldType = self::db()->getTableInfo('', 'type');
$this->initialize(); $this->initialize();
$this->relation = new Relation($this); $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)); $name = !empty($class->name) ? $class->name : basename(str_replace('\\', '/', $model));
self::$links[$model]->name($name); self::$links[$model]->name($name);
} }
// 设置当前模型 确保查询返回模型对象 // 设置当前模型 确保查询返回模型对象
self::$links[$model]->model($model); self::$links[$model]->model($model);
// 返回当前数据库对象 // 返回当前数据库对象
@@ -930,6 +934,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$value = NOW_TIME; $value = NOW_TIME;
break; break;
} }
} elseif (isset($this->fieldType[$name]) && preg_match('/(datetime|timestamp)/is', $this->fieldType[$name])) {
$value = date($this->dateFormat, NOW_TIME);
} else { } else {
$value = NOW_TIME; $value = NOW_TIME;
} }
@@ -965,7 +971,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
break; break;
case 'timestamp': case 'timestamp':
$format = !empty($param) ? $param : $this->dateFormat; $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; break;
case 'object': case 'object':
if (is_object($value)) { if (is_object($value)) {