From ec4f187aab881cd950a544deb01c96f3e142a631 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 22 Jan 2016 12:34:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BDb=E7=B1=BB=20traits\Query=20?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=B8=BA=20traits\Transaction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- library/think/Db.php | 11 +++++++++-- library/think/Loader.php | 2 +- library/think/Model.php | 5 +---- library/think/cache/driver/Db.php | 2 +- library/think/model/Adv.php | 4 ++-- library/traits/model/{Query.php => Transaction.php} | 2 +- 7 files changed, 16 insertions(+), 12 deletions(-) rename library/traits/model/{Query.php => Transaction.php} (99%) diff --git a/helper.php b/helper.php index c5b0fb4d..7f7df84d 100644 --- a/helper.php +++ b/helper.php @@ -93,7 +93,7 @@ function D($name = '', $layer = MODEL_LAYER) */ function db($config = []) { - return \think\Db::instance($config); + return \think\Db::connect($config); } /** diff --git a/library/think/Db.php b/library/think/Db.php index 3c45bf7c..26734f00 100644 --- a/library/think/Db.php +++ b/library/think/Db.php @@ -26,13 +26,13 @@ class Db public static $executeTimes = 0; /** - * 取得数据库类实例 + * 数据库初始化 并取得数据库类实例 * @static * @access public * @param mixed $config 连接配置 * @return Object 返回数据库驱动类 */ - public static function instance($config = []) + public static function connect($config = []) { $md5 = md5(serialize($config)); if (!isset(self::$instances[$md5])) { @@ -63,6 +63,9 @@ class Db $status = Config::get('app_status'); $config = $config[$status ?: 'default']; } + } elseif (is_string($config) && false === strpos($config, '/')) { + // 支持读取配置参数 + $config = Config::get($config); } if (is_string($config)) { return self::parseDsn($config); @@ -106,6 +109,10 @@ class Db // 调用驱动类的方法 public static function __callStatic($method, $params) { + if (is_null(self::$instance)) { + // 自动初始化数据库 + self::connect(); + } return call_user_func_array([self::$instance, $method], $params); } } diff --git a/library/think/Loader.php b/library/think/Loader.php index 5e6a24aa..84ce850e 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -325,7 +325,7 @@ class Loader */ public static function db($config = []) { - return Db::instance($config); + return Db::connect($config); } /** diff --git a/library/think/Model.php b/library/think/Model.php index 5c9f2d7a..21deae1a 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -983,11 +983,8 @@ class Model // 创建一个新的实例 if (is_string($linkId) && '' == $config) { $config = Config::get($linkId); - } elseif (!empty($config) && is_string($config) && false === strpos($config, '/')) { - // 支持读取配置参数 - $config = Config::get($config); } - $this->links[$linkId] = Db::instance($config); + $this->links[$linkId] = Db::connect($config); } elseif (null === $config) { $this->links[$linkId]->close(); // 关闭数据库连接 unset($this->links[$linkId]); diff --git a/library/think/cache/driver/Db.php b/library/think/cache/driver/Db.php index 5dd46777..d428788b 100644 --- a/library/think/cache/driver/Db.php +++ b/library/think/cache/driver/Db.php @@ -46,7 +46,7 @@ class Db if (!empty($options)) { $this->options = array_merge($this->options, $options); } - $this->handler = \Think\Db::instance(); + $this->handler = \Think\Db::connect(); } /** diff --git a/library/think/model/Adv.php b/library/think/model/Adv.php index fe99f771..da02eb2f 100644 --- a/library/think/model/Adv.php +++ b/library/think/model/Adv.php @@ -12,10 +12,10 @@ namespace think\model; T('model/Adv'); -T('model/Query'); +T('model/Transaction'); class Adv extends \think\Model { use \traits\model\Adv; - use \traits\model\Query; + use \traits\model\Transaction; } diff --git a/library/traits/model/Query.php b/library/traits/model/Transaction.php similarity index 99% rename from library/traits/model/Query.php rename to library/traits/model/Transaction.php index bd0b3514..8e9533bb 100644 --- a/library/traits/model/Query.php +++ b/library/traits/model/Transaction.php @@ -11,7 +11,7 @@ namespace traits\model; -trait Query +trait Transaction { /**