From 6172303c972b78175dafddca3d87155846f50e86 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 23 May 2016 11:47:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20database.auto=5Ftimestamp?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E5=8F=82=E6=95=B0=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=98=AF=E5=90=A6=E9=9C=80=E8=A6=81=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=86=99=E5=85=A5=E6=97=B6=E9=97=B4=E6=88=B3=E5=AD=97?= =?UTF-8?q?=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- convention.php | 34 ++++++++++++++++++--------------- library/think/Model.php | 6 +++++- library/think/db/Connection.php | 6 ++++-- library/think/db/Query.php | 17 ++++++++++++++--- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/convention.php b/convention.php index 10858064..8e2f20a1 100644 --- a/convention.php +++ b/convention.php @@ -173,35 +173,39 @@ return [ 'database' => [ // 数据库类型 - 'type' => 'mysql', + 'type' => 'mysql', // 数据库连接DSN配置 - 'dsn' => '', + 'dsn' => '', // 服务器地址 - 'hostname' => 'localhost', + 'hostname' => 'localhost', // 数据库名 - 'database' => '', + 'database' => '', // 数据库用户名 - 'username' => 'root', + 'username' => 'root', // 数据库密码 - 'password' => '', + 'password' => '', // 数据库连接端口 - 'hostport' => '', + 'hostport' => '', // 数据库连接参数 - '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, + // 自动写入时间戳字段 + 'auto_timestamp' => false, ], //分页配置 'paginate' => [ diff --git a/library/think/Model.php b/library/think/Model.php index e51c44f4..be01b26b 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -64,7 +64,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 更新自动完成列表 protected $update = []; // 是否需要自动写入时间戳 - protected $autoWriteTimestamp = true; + protected $autoWriteTimestamp; // 创建时间字段 protected $createTime = 'create_time'; // 更新时间字段 @@ -111,6 +111,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $this->name = basename(str_replace('\\', '/', $this->class)); } + if (is_null($this->autoWriteTimestamp)) { + $this->autoWriteTimestamp = $this->getConfig('auto_timestamp'); + } + // 执行初始化操作 $this->initialize(); } diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index dfa9ca49..e613e882 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -99,6 +99,8 @@ abstract class Connection 'fields_strict' => true, // 数据集返回类型 'resultset_type' => Db::RESULTSET_ARRAY, + // 自动写入时间戳字段 + 'auto_timestamp' => false, ]; // PDO连接参数 @@ -212,9 +214,9 @@ abstract class Connection * @param string $config 配置名称 * @return mixed */ - public function getConfig($config) + public function getConfig($config = '') { - return $this->config[$config]; + return $config ? $this->config[$config] : $this->config; } /** diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 9ed1cbd8..225500fb 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -124,7 +124,7 @@ class Query { if ($name || empty($this->table)) { $name = $name ?: $this->name; - $tableName = $this->connection->getConfig('prefix'); + $tableName = $this->getConfig('prefix'); if ($name) { $tableName .= Loader::parseName($name); } @@ -245,6 +245,17 @@ class Query return $this->connection->batchQuery($sql); } + /** + * 获取数据库的配置参数 + * @access public + * @param string $name 参数名称 + * @return boolean + */ + public function getConfig($name = '') + { + return $this->connection->getConfig($name); + } + /** * 获取当前的builder实例对象 * @access protected @@ -532,7 +543,7 @@ class Query } } } else { - $prefix = $this->connection->getConfig('prefix'); + $prefix = $this->getConfig('prefix'); // 传入的表名为数组 if (is_array($join)) { if (0 !== $key = key($join)) { @@ -1788,7 +1799,7 @@ class Query } if (!isset($options['strict'])) { - $options['strict'] = $this->connection->getConfig('fields_strict'); + $options['strict'] = $this->getConfig('fields_strict'); } foreach (['master', 'lock', 'fetch_class', 'fetch_sql', 'distinct'] as $name) {