改进Model类

This commit is contained in:
thinkphp
2016-01-21 19:11:48 +08:00
parent bb0bbe9425
commit 83f29db4f0

View File

@@ -1167,9 +1167,8 @@ class Model
protected function parseSql($sql) protected function parseSql($sql)
{ {
// 分析表达式 // 分析表达式
$sql = strtr($sql, ['__TABLE__' => $this->getTableName(), '__PREFIX__' => $this->tablePrefix]); $sql = strtr($sql, ['__TABLE__' => $this->getTableName(), '__PREFIX__' => $this->tablePrefix]);
$prefix = $this->tablePrefix; $sql = $this->parseSqlTable($sql);
$sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $sql);
$this->db->setModel($this->name); $this->db->setModel($this->name);
return $sql; return $sql;
} }
@@ -1205,16 +1204,15 @@ class Model
*/ */
public function _join($join, $type = 'INNER') public function _join($join, $type = 'INNER')
{ {
$prefix = $this->tablePrefix;
if (is_array($join)) { if (is_array($join)) {
foreach ($join as $key => &$_join) { foreach ($join as $key => &$_join) {
$_join = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $_join); $_join = $this->parseSqlTable($_join);
$_join = false !== stripos($_join, 'JOIN') ? $_join : $type . ' JOIN ' . $_join; $_join = false !== stripos($_join, 'JOIN') ? $_join : $type . ' JOIN ' . $_join;
} }
$this->options['join'] = $join; $this->options['join'] = $join;
} elseif (!empty($join)) { } elseif (!empty($join)) {
//将__TABLE_NAME__字符串替换成带前缀的表名 //将__TABLE_NAME__字符串替换成带前缀的表名
$join = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $join); $join = $this->parseSqlTable($join);
$this->options['join'][] = false !== stripos($join, 'JOIN') ? $join : $type . ' JOIN ' . $join; $this->options['join'][] = false !== stripos($join, 'JOIN') ? $join : $type . ' JOIN ' . $join;
} }
return $this; return $this;
@@ -1270,9 +1268,7 @@ class Model
$join = trim($join); $join = trim($join);
if (0 === strpos($join, '__')) { if (0 === strpos($join, '__')) {
//将__TABLE_NAME__字符串替换成带前缀的表名 //将__TABLE_NAME__字符串替换成带前缀的表名
$table = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) { $table = $this->parseSqlTable($join);
return $prefix . strtolower($match[1]);
}, $join);
} elseif (false === strpos($join, '(') && !empty($prefix) && 0 !== strpos($join, $prefix)) { } elseif (false === strpos($join, '(') && !empty($prefix) && 0 !== strpos($join, $prefix)) {
// 传入的表名中不带有'('并且不以默认的表前缀开头时加上默认的表前缀 // 传入的表名中不带有'('并且不以默认的表前缀开头时加上默认的表前缀
$table = $prefix . $join; $table = $prefix . $join;
@@ -1309,9 +1305,8 @@ class Model
} }
// 转换union表达式 // 转换union表达式
if (is_string($union)) { if (is_string($union)) {
$prefix = $this->tablePrefix;
//将__TABLE_NAME__字符串替换成带前缀的表名 //将__TABLE_NAME__字符串替换成带前缀的表名
$options = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $union); $options = $this->parseSqlTable($union);
} elseif (is_array($union)) { } elseif (is_array($union)) {
if (isset($union[0])) { if (isset($union[0])) {
$this->options['union'] = array_merge($this->options['union'], $union); $this->options['union'] = array_merge($this->options['union'], $union);
@@ -1474,12 +1469,11 @@ class Model
*/ */
public function table($table) public function table($table)
{ {
$prefix = $this->tablePrefix;
if (is_array($table)) { if (is_array($table)) {
$this->options['table'] = $table; $this->options['table'] = $table;
} elseif (!empty($table)) { } elseif (!empty($table)) {
//将__TABLE_NAME__替换成带前缀的表名 //将__TABLE_NAME__替换成带前缀的表名
$table = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $table); $table = $this->parseSqlTable($table);
$this->options['table'] = $table; $this->options['table'] = $table;
} }
return $this; return $this;
@@ -1493,12 +1487,11 @@ class Model
*/ */
public function using($using) public function using($using)
{ {
$prefix = $this->tablePrefix;
if (is_array($using)) { if (is_array($using)) {
$this->options['using'] = $using; $this->options['using'] = $using;
} elseif (!empty($using)) { } elseif (!empty($using)) {
//将__TABLE_NAME__替换成带前缀的表名 //将__TABLE_NAME__替换成带前缀的表名
$using = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {return $prefix . strtolower($match[1]);}, $using); $using = $this->parseSqlTable($using);
$this->options['using'] = $using; $this->options['using'] = $using;
} }
return $this; return $this;
@@ -1694,4 +1687,22 @@ class Model
$this->options['master'] = true; $this->options['master'] = true;
return $this; return $this;
} }
/**
* 将SQL语句中的__TABLE_NAME__字符串替换成带前缀的表名小写
* @access protected
* @param string $sql sql语句
* @return string
*/
protected function parseSqlTable($sql)
{
if (false !== strpos($sql, '__')) {
$prefix = $this->tablePrefix;
return preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function ($match) use ($prefix) {
return $prefix . strtolower($match[1]);
}, $sql);
} else {
return $sql;
}
}
} }