ThinkPHP 3.2.3 数据表大小写问题(查询时候强制转换小写)解决办法

时间:2022-09-23 23:30:23

ThinkPHP官方解决方案:

//在配置中加入
'DB_PARAMS' => array(\PDO::ATTR_CASE => \PDO::CASE_NATURAL), // 数据库连接参数

亲测行不通 (或许是我操作问题,如果有同样问题的同学,请接着往下看)

//ThinkPHP/Common/funcitons.php
//line 457  return trim(preg_replace("/[A-Z]/", "\\0", $name), "_");  //去掉strtolower(),把"_\\0"中"_"去掉
function parse_name($name, $type=0) {
    if ($type) {
        return ucfirst(preg_replace_callback('/_([a-zA-Z])/', function($match){return strtoupper($match[1]);}, $name));
    } else {
//        return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
        return trim(preg_replace("/[A-Z]/", "\\0", $name), "_");
    }
}

//ThinkPHP/library/Think/Model.class.php
//line 1470  $this->trueTableName = $tableName;  //去掉strtolower
    public function getTableName() {
        if(empty($this->trueTableName)) {
            $tableName  = !empty($this->tablePrefix) ? $this->tablePrefix : '';
            if(!empty($this->tableName)) {
                $tableName .= $this->tableName;
            }else{
                $tableName .= parse_name($this->name);
            }
//            $this->trueTableName    =   strtolower($tableName);
            $this->trueTableName    =   $tableName;
        }
        return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName;
    }

最后再来个  mysql 不能创建大写的表名问题

[mysqld]
#(0:区分;1:不区分)数据表区分大小写
lower_case_table_names=0