改进join方法解析

This commit is contained in:
thinkphp
2016-10-06 21:50:10 +08:00
parent 32e001cdc6
commit 7c49bd4420
2 changed files with 7 additions and 12 deletions

View File

@@ -179,11 +179,8 @@ abstract class Builder
*/ */
protected function parseTable($tables, $options = []) protected function parseTable($tables, $options = [])
{ {
if (is_string($tables)) {
$tables = (array) $tables;
}
$item = []; $item = [];
foreach ($tables as $table) { foreach ((array) $tables as $table) {
$table = $this->parseSqlTable($table); $table = $this->parseSqlTable($table);
if (isset($options['alias'][$table])) { if (isset($options['alias'][$table])) {
$item[] = $this->parseKey($table) . ' ' . $this->parseKey($options['alias'][$table]); $item[] = $this->parseKey($table) . ' ' . $this->parseKey($options['alias'][$table]);
@@ -438,13 +435,14 @@ abstract class Builder
if (!empty($join)) { if (!empty($join)) {
foreach ($join as $item) { foreach ($join as $item) {
list($table, $type, $on) = $item; list($table, $type, $on) = $item;
if (!empty($options['alias'])) { $condition = [];
foreach ($options['alias'] as $key => $val) { foreach ((array) $on as $val) {
$on = str_replace($key . '.', $this->parseKey($val, $options) . '.', $on); list($val1, $val2) = explode('=', $val, 2);
} $condition[] = $this->parseKey($val1, $options) . '=' . $this->parseKey($val2, $options);
} }
$table = $this->parseTable($table, $options); $table = $this->parseTable($table, $options);
$joinStr .= ' ' . $type . ' JOIN ' . $table . ' ON ' . $on; $joinStr .= ' ' . $type . ' JOIN ' . $table . ' ON ' . implode(' AND ', $condition);
} }
} }
return $joinStr; return $joinStr;

View File

@@ -696,9 +696,6 @@ class Query
$this->alias([$table => $alias]); $this->alias([$table => $alias]);
} }
} }
if (is_array($condition)) {
$condition = implode(' AND ', $condition);
}
$this->options['join'][] = [$table, strtoupper($type), $condition]; $this->options['join'][] = [$table, strtoupper($type), $condition];
} }
return $this; return $this;