function M($name='', $tablePrefix='',$connection='') {
static $_model = array();
if(strpos($name,':')) {
list($class,$name) = explode(':',$name);
}else{
$class = 'Model';
}
$guid = $tablePrefix . $name . '_' . $class;
if (!isset($_model[$guid]))
$_model[$guid] = new $class($name,$tablePrefix,$connection);
return $_model[$guid];
}这是thinkphp的M方法如果是下面的情况:
M(null, null, 'DB_CONFIG1')->table('ofUser')->find();
M(null, null, 'DB_CONFIG2')->table('ofUser2')->find();DB_CONFIG2将切换失败解决方案:
单例模式的静态变量$_model的键值加上DB_CONFIG的标识
最佳答案