tp数据库表大写命名的一些问题
生活随笔
收集整理的這篇文章主要介紹了
tp数据库表大写命名的一些问题
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
在使用thinkphp時(shí),如果數(shù)據(jù)庫(kù)表命名有大寫(xiě),會(huì)被轉(zhuǎn)換成小寫(xiě)加下劃線(可以使用$model->_sql())來(lái)查看實(shí)際執(zhí)行的sql是什么
這個(gè)問(wèn)題,看了一下源代碼,在 Thinkphp/Common/common.php里面,這個(gè)函數(shù)會(huì)將數(shù)據(jù)表(或視圖)的大寫(xiě)字母轉(zhuǎn)換為下劃線+小寫(xiě):
1 function parse_name($name, $type=0) { 2 if ($type) { 3 return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name)); 4 } else { 5 return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_")); 6 //<feixiang 2013年7月6日 這里會(huì)將數(shù)據(jù)庫(kù)表名里的大寫(xiě)轉(zhuǎn)換為 _小寫(xiě),這里不轉(zhuǎn)換,在Model.class.php還將表轉(zhuǎn)換成了小寫(xiě),需要改一下 > 7 //echo $name ; 8 //return $name; 9 } 10 }另外,在?Thinkphp/Core/Model.class.php里面,會(huì)將整個(gè)表名轉(zhuǎn)換成小寫(xiě):
1 public function getTableName() { 2 if(empty($this->trueTableName)) { 3 $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : ''; 4 if(!empty($this->tableName)) { 5 $tableName .= $this->tableName; 6 }else{ 7 $tableName .= parse_name($this->name); 8 } 9 //<feixiang 這里會(huì)將表名轉(zhuǎn)換成小寫(xiě),我們這里不轉(zhuǎn)換> 10 $this->trueTableName = strtolower($tableName); 11 //$this->trueTableName = $tableName; 12 } 13 return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName; 14 }這樣有好處——規(guī)范。
但是在我們的開(kāi)發(fā)中,有大寫(xiě)的數(shù)據(jù)表,所以改了一下(注釋的那些)...
或者可以在模型定義里面加上:
protected?$trueTableName?=?'myTableName';
來(lái)覆蓋$this->trueTableName
轉(zhuǎn)載于:https://my.oschina.net/ifeixiang/blog/339418
總結(jié)
以上是生活随笔為你收集整理的tp数据库表大写命名的一些问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: js中null和undefined
- 下一篇: linux cmake编译源码,linu