From 118800ed934ebd5d923e4eb579c78721be358f3c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 24 May 2016 11:34:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BQuery=E7=B1=BB=E7=9A=84view?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E6=94=AF=E6=8C=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=A1=A8=E5=AE=9A=E4=B9=89=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Query.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/library/think/db/Query.php b/library/think/db/Query.php index e463320e..1674f42c 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -651,33 +651,39 @@ class Query * @param string $type JOIN类型 * @return $this */ - public function view($join, $field, $on = null, $type = 'INNER') + public function view($join, $field = null, $on = null, $type = 'INNER') { $this->options['view'] = true; - if (is_array($join)) { + if (is_array($join) && is_null($field)) { foreach ($join as $key => $val) { $this->view($key, $val[0], isset($val[1]) ? $val[1] : null, isset($val[2]) ? $val[2] : 'INNER'); } } else { $fields = []; - $table = $this->getTable($join); + if (is_array($join)) { + // 支持数据表别名 + list($join, $alias) = $join; + } else { + $alias = $join; + } + $table = $this->getTable($join); if (is_string($field)) { $field = explode(',', $field); } foreach ($field as $key => $val) { if (is_numeric($key)) { - $fields[] = $join . '.' . $val; - $this->options['map'][$val] = $join . '.' . $val; + $fields[] = $alias . '.' . $val; + $this->options['map'][$val] = $alias . '.' . $val; } else { - $fields[] = $join . '.' . $key . ' AS ' . $val; - $this->options['map'][$val] = $join . '.' . $key; + $fields[] = $alias . '.' . $key . ' AS ' . $val; + $this->options['map'][$val] = $alias . '.' . $key; } } $this->field($fields); if ($on) { - $this->join($table . ' ' . $join, $on, $type); + $this->join($table . ' ' . $alias, $on, $type); } else { - $this->table($table . ' ' . $join); + $this->table($table . ' ' . $alias); } } return $this;