From 35250ef91c2f0a3a97a3cb30a2bdff5438ec6713 Mon Sep 17 00:00:00 2001 From: top_nuomi <1130395124@qq.com> Date: Thu, 5 Sep 2019 19:51:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DDatabase=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=8D=95=E4=BE=8B=E6=97=B6=E9=99=A4=E5=8E=BB=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E5=90=8C=E5=90=8D=E8=A1=A8=E5=8F=AF=E8=83=BD=E4=BC=9A=E5=86=B2?= =?UTF-8?q?=E7=AA=81=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/library/Database.php | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/framework/library/Database.php b/framework/library/Database.php index c4c4ff0..9e67c5a 100644 --- a/framework/library/Database.php +++ b/framework/library/Database.php @@ -105,8 +105,8 @@ class Database private function __construct($table, $pk, $prefix) { $driver = Register::get('DBDriver'); - $this->config = $config = Config::instance()->get('db'); - $this->table = (($prefix) ? $prefix : $config['prefix']) . $table; + $this->config = Config::instance()->get('db'); + $this->table = $this->getTableName($prefix, $table); $this->pk = $pk; $this->setDriver($driver, $this->config); } @@ -121,10 +121,11 @@ class Database */ public static function table($table, $pk = '', $prefix = '') { - if (!isset(self::$instance[$table])) { - self::$instance[$table] = new self($table, $pk, $prefix); + $ident = md5($prefix . $table); + if (!isset(self::$instance[$ident])) { + self::$instance[$ident] = new self($table, $pk, $prefix); } - return self::$instance[$table]; + return self::$instance[$ident]; } /** @@ -138,6 +139,25 @@ class Database self::$driver = $driver->connect($config); } + /** + * 获取表名 + * @param $prefix + * @param $table + * @return string + */ + private function getTableName($prefix, $table) + { + // 无前缀 + if ($prefix === false) { + $tableName = $table; + } elseif (!$prefix) { + $tableName = $this->config['prefix'] . $table; + } else { + $tableName = $prefix . $table; + } + return $tableName; + } + /** * 指定多张表 * @param $effect