改进table方法的解析

This commit is contained in:
thinkphp
2016-10-06 20:37:12 +08:00
parent e6976918de
commit 32e001cdc6
2 changed files with 22 additions and 15 deletions

View File

@@ -173,29 +173,25 @@ abstract class Builder
/** /**
* table分析 * table分析
* @access protected * @access protected
* @param mixed $table * @param mixed $tables
* @param array $options * @param array $options
* @return string * @return string
*/ */
protected function parseTable($tables, $options = []) protected function parseTable($tables, $options = [])
{ {
if (is_array($tables)) { if (is_string($tables)) {
// 支持别名定义 $tables = (array) $tables;
foreach ($tables as $table => $alias) { }
$array[] = !is_numeric($table) ? $item = [];
$this->parseKey($table) . ' ' . $this->parseKey($alias) : foreach ($tables as $table) {
$this->parseKey($alias); $table = $this->parseSqlTable($table);
} if (isset($options['alias'][$table])) {
$tables = implode(',', $array); $item[] = $this->parseKey($table) . ' ' . $this->parseKey($options['alias'][$table]);
} elseif (is_string($tables)) {
$tables = $this->parseSqlTable($tables);
if (isset($options['alias'][$tables])) {
$tables = $this->parseKey($tables) . ' ' . $this->parseKey($options['alias'][$tables]);
} else { } else {
$tables = $this->parseKey($tables); $item[] = $this->parseKey($table);
} }
} }
return $tables; return implode(',', $item);
} }
/** /**

View File

@@ -1049,6 +1049,17 @@ class Query
list($table, $alias) = explode(' ', $table); list($table, $alias) = explode(' ', $table);
$this->alias([$table => $alias]); $this->alias([$table => $alias]);
} }
} else {
$tables = $table;
$table = [];
foreach ($tables as $key => $val) {
if (is_numeric($key)) {
$table[] = $val;
} else {
$this->alias([$key => $val]);
$table[] = $key;
}
}
} }
$this->options['table'] = $table; $this->options['table'] = $table;
return $this; return $this;