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>
// +----------------------------------------------------------------------
define('THINK_VERSION', '5.0.4');
define('THINK_VERSION', '5.0.5beta');
define('THINK_START_TIME', microtime(true));
define('THINK_START_MEM', memory_get_usage());
define('EXT', '.php');

View File

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

View File

@@ -65,47 +65,49 @@ abstract class Connection
// 数据库连接参数配置
protected $config = [
// 数据库类型
'type' => '',
'type' => '',
// 服务器地址
'hostname' => '',
'hostname' => '',
// 数据库名
'database' => '',
'database' => '',
// 用户名
'username' => '',
'username' => '',
// 密码
'password' => '',
'password' => '',
// 端口
'hostport' => '',
'hostport' => '',
// 连接dsn
'dsn' => '',
'dsn' => '',
// 数据库连接参数
'params' => [],
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
'charset' => 'utf8',
// 数据库表前缀
'prefix' => '',
'prefix' => '',
// 数据库调试模式
'debug' => false,
'debug' => false,
// 数据库部署方式: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_explain' => false,
'sql_explain' => false,
// Builder类
'builder' => '',
'builder' => '',
// Query类
'query' => '\\think\\db\\Query',
'query' => '\\think\\db\\Query',
];
// PDO连接参数