修复Database获取单例时除去前缀同名表可能会冲突的错误
This commit is contained in:
parent
a329ff4e68
commit
35250ef91c
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue