Model类的autoWriteTimestamp属性和类型转化类型支持设置为类名

dateFormat属性支持在数据库配置中设置datetime_format参数
This commit is contained in:
thinkphp
2016-12-22 17:37:52 +08:00
parent 91e089b7e7
commit 6eedbc5a0a
3 changed files with 43 additions and 23 deletions

View File

@@ -9,7 +9,7 @@
// | Author: liu21st <liu21st@gmail.com> // | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
define('THINK_VERSION', '5.0.4'); define('THINK_VERSION', '5.0.5beta');
define('THINK_START_TIME', microtime(true)); define('THINK_START_TIME', microtime(true));
define('THINK_START_MEM', memory_get_usage()); define('THINK_START_MEM', memory_get_usage());
define('EXT', '.php'); define('EXT', '.php');

View File

@@ -96,7 +96,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
// 更新时间字段 // 更新时间字段
protected $updateTime = 'update_time'; protected $updateTime = 'update_time';
// 时间字段取出后的默认时间格式 // 时间字段取出后的默认时间格式
protected $dateFormat = 'Y-m-d H:i:s'; protected $dateFormat;
// 字段类型或者格式转换 // 字段类型或者格式转换
protected $type = []; protected $type = [];
// 是否为更新数据 // 是否为更新数据
@@ -154,6 +154,11 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$this->autoWriteTimestamp = $this->db(false)->getConfig('auto_timestamp'); $this->autoWriteTimestamp = $this->db(false)->getConfig('auto_timestamp');
} }
if (is_null($this->dateFormat)) {
// 设置时间戳格式
$this->dateFormat = $this->db(false)->getConfig('datetime_format');
}
// 执行初始化操作 // 执行初始化操作
$this->initialize(); $this->initialize();
} }
@@ -329,12 +334,21 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case 'integer': case 'integer':
default: default:
$value = $_SERVER['REQUEST_TIME']; $value = $_SERVER['REQUEST_TIME'];
if (strpos($type, '\\')) {
// 传入类名
$value = new $type($value);
}
break; break;
} }
} elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) { } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), ['datetime', 'date', 'timestamp'])) {
$value = date($this->dateFormat, $_SERVER['REQUEST_TIME']); $value = date($this->dateFormat, $_SERVER['REQUEST_TIME']);
} else { } else {
$value = $_SERVER['REQUEST_TIME']; $value = $_SERVER['REQUEST_TIME'];
if (is_string($this->autoWriteTimestamp) && strpos($this->autoWriteTimestamp, '\\')) {
// 传入类名
$class = $this->autoWriteTimestamp;
$value = new $class($value);
}
} }
return $value; return $value;
} }
@@ -484,6 +498,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
case 'serialize': case 'serialize':
$value = unserialize($value); $value = unserialize($value);
break; break;
default:
if (strpos($type, '\\')) {
$value = new $type($value);
}
} }
return $value; return $value;
} }

View File

@@ -100,6 +100,8 @@ abstract class Connection
'resultset_type' => 'array', 'resultset_type' => 'array',
// 自动写入时间戳字段 // 自动写入时间戳字段
'auto_timestamp' => false, 'auto_timestamp' => false,
// 时间戳格式
'datetime_format' => 'Y-m-d H:i:s',
// 是否需要进行SQL性能分析 // 是否需要进行SQL性能分析
'sql_explain' => false, 'sql_explain' => false,
// Builder类 // Builder类