thinkphp M方法切换数据库bug

浏览:3821 发布日期:2015/06/11 分类:技术分享 关键字: thinkphp BUG
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的标识
最佳答案
评论( 相关
后面还有条评论,点击查看>>