修复Database获取单例时除去前缀同名表可能会冲突的错误

This commit is contained in:
TOP糯米 2019-09-05 19:51:17 +08:00
parent a329ff4e68
commit 35250ef91c
1 changed files with 25 additions and 5 deletions

View File

@ -105,8 +105,8 @@ class Database
private function __construct($table, $pk, $prefix) private function __construct($table, $pk, $prefix)
{ {
$driver = Register::get('DBDriver'); $driver = Register::get('DBDriver');
$this->config = $config = Config::instance()->get('db'); $this->config = Config::instance()->get('db');
$this->table = (($prefix) ? $prefix : $config['prefix']) . $table; $this->table = $this->getTableName($prefix, $table);
$this->pk = $pk; $this->pk = $pk;
$this->setDriver($driver, $this->config); $this->setDriver($driver, $this->config);
} }
@ -121,10 +121,11 @@ class Database
*/ */
public static function table($table, $pk = '', $prefix = '') public static function table($table, $pk = '', $prefix = '')
{ {
if (!isset(self::$instance[$table])) { $ident = md5($prefix . $table);
self::$instance[$table] = new self($table, $pk, $prefix); 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); 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 * @param $effect