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

@@ -65,47 +65,49 @@ abstract class Connection
// 数据库连接参数配置 // 数据库连接参数配置
protected $config = [ protected $config = [
// 数据库类型 // 数据库类型
'type' => '', 'type' => '',
// 服务器地址 // 服务器地址
'hostname' => '', 'hostname' => '',
// 数据库名 // 数据库名
'database' => '', 'database' => '',
// 用户名 // 用户名
'username' => '', 'username' => '',
// 密码 // 密码
'password' => '', 'password' => '',
// 端口 // 端口
'hostport' => '', 'hostport' => '',
// 连接dsn // 连接dsn
'dsn' => '', 'dsn' => '',
// 数据库连接参数 // 数据库连接参数
'params' => [], 'params' => [],
// 数据库编码默认采用utf8 // 数据库编码默认采用utf8
'charset' => 'utf8', 'charset' => 'utf8',
// 数据库表前缀 // 数据库表前缀
'prefix' => '', 'prefix' => '',
// 数据库调试模式 // 数据库调试模式
'debug' => false, 'debug' => false,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0, 'deploy' => 0,
// 数据库读写是否分离 主从式有效 // 数据库读写是否分离 主从式有效
'rw_separate' => false, 'rw_separate' => false,
// 读写分离后 主服务器数量 // 读写分离后 主服务器数量
'master_num' => 1, 'master_num' => 1,
// 指定从服务器序号 // 指定从服务器序号
'slave_no' => '', 'slave_no' => '',
// 是否严格检查字段是否存在 // 是否严格检查字段是否存在
'fields_strict' => true, 'fields_strict' => true,
// 数据集返回类型 // 数据集返回类型
'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类
'builder' => '', 'builder' => '',
// Query类 // Query类
'query' => '\\think\\db\\Query', 'query' => '\\think\\db\\Query',
]; ];
// PDO连接参数 // PDO连接参数