Thinkphp table 传入数组 不会自动添加表前缀

浏览:3343 发布日期:2015/01/22 分类:技术分享
Thinkphp table 传入数组 不会自动添加表前缀
用连表查询的时候出现了一点小问题。
table 中数组传入表名不会自动添加表前缀。。$result=M()->table(array('auth_rule_group'=>'rule_group','auth_rule'=>'rule'))->where('rule_group.id=rule.group_id')->find();就改写了一下thinkphp的model文件
\ThinkPHP\Library\Think\Model.class.phppublic function table($table) {
        $prefix =   $this->tablePrefix;
        if(is_array($table)) {
            /*数组添加表前缀*/
            foreach ($table as $key => $value) {
                $table[C('DB_PREFIX').$key]=$value;
                unset($table[$key]);
            }
            $this->options['table'] =   $table;
        }elseif(!empty($table)) {
            //将__TABLE_NAME__替换成带前缀的表名
            $table  = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $table);
            $this->options['table'] =   $table;
        }
        return $this;
    }
不知道是不是最好的写法呢? 反正解决了问题! 希望下个版本THINKPHP能改进下!
最佳答案
评论( 相关
后面还有条评论,点击查看>>