diff --git a/library/think/Db.php b/library/think/Db.php index 832ed66d..a8a86972 100644 --- a/library/think/Db.php +++ b/library/think/Db.php @@ -28,23 +28,30 @@ class Db * @static * @access public * @param mixed $config 连接配置 - * @return Object 返回数据库驱动类 + * @param bool|string $name 连接标识 true 强制重新连接 + * @return \think\db\Connection */ - public static function connect($config = []) + public static function connect($config = [], $name = false) { - $md5 = md5(serialize($config)); - if (!isset(self::$instances[$md5])) { + if (false === $name) { + $name = md5(serialize($config)); + } + if (true === $name || !isset(self::$instances[$name])) { // 解析连接参数 支持数组和字符串 $options = self::parseConfig($config); if (empty($options['type'])) { throw new Exception('db type error'); } - $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\connector\\') . ucwords($options['type']); - self::$instances[$md5] = new $class($options); + $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\connector\\') . ucwords($options['type']); // 记录初始化信息 APP_DEBUG && Log::record('[ DB ] INIT ' . $options['type'] . ':' . var_export($options, true), 'info'); + if (true === $name) { + return new $class($options); + } else { + self::$instances[$name] = new $class($options); + } } - return self::$instances[$md5]; + return self::$instances[$name]; } /** diff --git a/library/think/Model.php b/library/think/Model.php index a7547429..83aad670 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -834,7 +834,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess $model = get_called_class(); $class = new static; if (!isset(self::$links[$model])) { - self::$links[$model] = Db::connect($class->connection); + self::$links[$model] = Db::connect($class->connection, $model); } // 设置当前数据表和模型名 if (!empty($class->table)) { diff --git a/library/think/db/Query.php b/library/think/db/Query.php index c7ae209e..6c7e4973 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -39,7 +39,7 @@ class Query */ public function __construct($connection = '') { - $this->connection = $connection ?: Db::connect(); + $this->connection = $connection ?: Db::connect([], true); $this->driver = $this->connection->getDriverName(); } @@ -988,7 +988,7 @@ class Query $i = 0; $currentModel = $this->options['model']; - $class = new $this->options['model']; + $class = new $currentModel; foreach ($with as $key => $relation) { $closure = false; if ($relation instanceof \Closure) { @@ -1005,15 +1005,15 @@ class Query $info = $class->getRelationInfo(); if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) { if (0 == $i) { - $joinName = Loader::parseName(basename(str_replace('\\', '/', $this->options['model']))); - $joinTable = $this->getTable(); - $this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName); + $name = Loader::parseName(basename(str_replace('\\', '/', $currentModel))); + $table = $this->getTable(); + $this->table($table)->alias($name)->field(true, false, $table, $name); } // 预载入封装 - $table = $info['model']::getTable(); - $name = Loader::parseName(basename(str_replace('\\', '/', $info['model']))); - $this->via($name); - $this->join($table . ' ' . $name, $joinName . '.' . $info['localKey'] . '=' . $name . '.' . $info['foreignKey'])->field(true, false, $table, $name, $name . '__'); + $joinTable = $model->getTable(); + $joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model']))); + $this->via($joinName); + $this->join($joinTable . ' ' . $joinName, $name . '.' . $info['localKey'] . '=' . $joinName . '.' . $info['foreignKey'])->field(true, false, $joinTable, $joinName, $joinName . '__'); if ($closure) { // 执行闭包查询 call_user_func_array($closure, [ & $this]); @@ -1024,7 +1024,6 @@ class Query } } $this->via(); - $this->model($currentModel); $this->options['with'] = $with; return $this; }