From 46009e3c2e0efbbd773150539cffc73d2ff6c07d Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 10 May 2016 15:17:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BModel=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index f0c693a4..d02cbac8 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -28,6 +28,8 @@ abstract class Model implements \JsonSerializable, \ArrayAccess // 数据库对象池 private static $links = []; + // 对象实例 + private static $instance = []; // 数据库配置 protected $connection = []; // 当前模型名称 @@ -858,10 +860,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess public static function db() { $model = get_called_class(); - $class = new static; + if (!isset(self::$links[$model])) { - self::$links[$model] = Db::connect($class->connection, $model); + $class = new static; + self::$links[$model] = Db::connect($class->connection, $model); + self::$instance[$model] = $class; + } else { + $class = self::$instance[$model]; } + // 设置当前数据表和模型名 if (!empty($class->table)) { self::$links[$model]->table($class->table);