From 32e001cdc68ef41308b2d80b21290f5555d6309f Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 6 Oct 2016 20:37:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Btable=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Builder.php | 26 +++++++++++--------------- library/think/db/Query.php | 11 +++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/library/think/db/Builder.php b/library/think/db/Builder.php index 4f57c84a..80defb75 100644 --- a/library/think/db/Builder.php +++ b/library/think/db/Builder.php @@ -173,29 +173,25 @@ abstract class Builder /** * table分析 * @access protected - * @param mixed $table + * @param mixed $tables * @param array $options * @return string */ protected function parseTable($tables, $options = []) { - if (is_array($tables)) { - // 支持别名定义 - foreach ($tables as $table => $alias) { - $array[] = !is_numeric($table) ? - $this->parseKey($table) . ' ' . $this->parseKey($alias) : - $this->parseKey($alias); - } - $tables = implode(',', $array); - } elseif (is_string($tables)) { - $tables = $this->parseSqlTable($tables); - if (isset($options['alias'][$tables])) { - $tables = $this->parseKey($tables) . ' ' . $this->parseKey($options['alias'][$tables]); + if (is_string($tables)) { + $tables = (array) $tables; + } + $item = []; + foreach ($tables as $table) { + $table = $this->parseSqlTable($table); + if (isset($options['alias'][$table])) { + $item[] = $this->parseKey($table) . ' ' . $this->parseKey($options['alias'][$table]); } else { - $tables = $this->parseKey($tables); + $item[] = $this->parseKey($table); } } - return $tables; + return implode(',', $item); } /** diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 1af39e58..c6b49ebf 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -1049,6 +1049,17 @@ class Query list($table, $alias) = explode(' ', $table); $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; return $this;