TP在Model层定义表名时,有空格会报错!

浏览:1201 发布日期:2015/09/25 分类:技术分享
最近在做项目是用到了TP,因为习惯操作在定义表名时,加了个空格!
例如:
class yourModel extends Model {
protected $tableName = " t_yourtable";
}
因此在实例化是总是报错,报表名不存在!
报错:
SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name

检查了半天也没有找出原因,最后已一行一行代码调试,才弄明白原来TP针对自定义表名没有进行空格符过滤,结果导致在对表名进行fields操作时,总是报空!

解决方案:
1、在model层自定义表名时,不要带有空格符。(但是这个很容易保证没有误操作,特别是在多人项目中)
2、修改TP内核代码中的Model.class.php中的getTableName方法
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);
}
return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName;
}
改为
public function getTableName() {
if(empty($this->trueTableName)) {
$tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
if(!empty($this->tableName)) {
$tableName .= trim($this->tableName);
}else{
$tableName .= parse_name($this->name);
}
$this->trueTableName = trim(strtolower($tableName));
}
return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName;
}


附件 QQ图片20150925174442.png ( 18.74 KB 下载:1 次 )

最佳答案
评论( 相关
后面还有条评论,点击查看>>