每个模型类采用 独立的数据库实例避免混淆

This commit is contained in:
thinkphp
2016-05-05 11:33:32 +08:00
parent fd72a3006c
commit 544e3ea833
3 changed files with 24 additions and 18 deletions

View File

@@ -28,23 +28,30 @@ class Db
* @static * @static
* @access public * @access public
* @param mixed $config 连接配置 * @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 (false === $name) {
if (!isset(self::$instances[$md5])) { $name = md5(serialize($config));
}
if (true === $name || !isset(self::$instances[$name])) {
// 解析连接参数 支持数组和字符串 // 解析连接参数 支持数组和字符串
$options = self::parseConfig($config); $options = self::parseConfig($config);
if (empty($options['type'])) { if (empty($options['type'])) {
throw new Exception('db type error'); throw new Exception('db type error');
} }
$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\connector\\') . ucwords($options['type']); $class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\connector\\') . ucwords($options['type']);
self::$instances[$md5] = new $class($options);
// 记录初始化信息 // 记录初始化信息
APP_DEBUG && Log::record('[ DB ] INIT ' . $options['type'] . ':' . var_export($options, true), 'info'); 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];
} }
/** /**

View File

@@ -834,7 +834,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$model = get_called_class(); $model = get_called_class();
$class = new static; $class = new static;
if (!isset(self::$links[$model])) { if (!isset(self::$links[$model])) {
self::$links[$model] = Db::connect($class->connection); self::$links[$model] = Db::connect($class->connection, $model);
} }
// 设置当前数据表和模型名 // 设置当前数据表和模型名
if (!empty($class->table)) { if (!empty($class->table)) {

View File

@@ -39,7 +39,7 @@ class Query
*/ */
public function __construct($connection = '') public function __construct($connection = '')
{ {
$this->connection = $connection ?: Db::connect(); $this->connection = $connection ?: Db::connect([], true);
$this->driver = $this->connection->getDriverName(); $this->driver = $this->connection->getDriverName();
} }
@@ -988,7 +988,7 @@ class Query
$i = 0; $i = 0;
$currentModel = $this->options['model']; $currentModel = $this->options['model'];
$class = new $this->options['model']; $class = new $currentModel;
foreach ($with as $key => $relation) { foreach ($with as $key => $relation) {
$closure = false; $closure = false;
if ($relation instanceof \Closure) { if ($relation instanceof \Closure) {
@@ -1005,15 +1005,15 @@ class Query
$info = $class->getRelationInfo(); $info = $class->getRelationInfo();
if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) { if (in_array($info['type'], [Relation::HAS_ONE, Relation::BELONGS_TO])) {
if (0 == $i) { if (0 == $i) {
$joinName = Loader::parseName(basename(str_replace('\\', '/', $this->options['model']))); $name = Loader::parseName(basename(str_replace('\\', '/', $currentModel)));
$joinTable = $this->getTable(); $table = $this->getTable();
$this->table($joinTable)->alias($joinName)->field(true, false, $joinTable, $joinName); $this->table($table)->alias($name)->field(true, false, $table, $name);
} }
// 预载入封装 // 预载入封装
$table = $info['model']::getTable(); $joinTable = $model->getTable();
$name = Loader::parseName(basename(str_replace('\\', '/', $info['model']))); $joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model'])));
$this->via($name); $this->via($joinName);
$this->join($table . ' ' . $name, $joinName . '.' . $info['localKey'] . '=' . $name . '.' . $info['foreignKey'])->field(true, false, $table, $name, $name . '__'); $this->join($joinTable . ' ' . $joinName, $name . '.' . $info['localKey'] . '=' . $joinName . '.' . $info['foreignKey'])->field(true, false, $joinTable, $joinName, $joinName . '__');
if ($closure) { if ($closure) {
// 执行闭包查询 // 执行闭包查询
call_user_func_array($closure, [ & $this]); call_user_func_array($closure, [ & $this]);
@@ -1024,7 +1024,6 @@ class Query
} }
} }
$this->via(); $this->via();
$this->model($currentModel);
$this->options['with'] = $with; $this->options['with'] = $with;
return $this; return $this;
} }