改进parseTable方法和parseJoin 关联预载入支持hasOne自关联

This commit is contained in:
thinkphp
2016-10-08 21:53:27 +08:00
parent b810c9a3ca
commit 6f9fc912df
2 changed files with 29 additions and 15 deletions

View File

@@ -180,12 +180,17 @@ abstract class Builder
protected function parseTable($tables, $options = []) protected function parseTable($tables, $options = [])
{ {
$item = []; $item = [];
foreach ((array) $tables as $table) { foreach ((array) $tables as $key => $table) {
$table = $this->parseSqlTable($table); if (!is_numeric($key)) {
if (isset($options['alias'][$table])) { $key = $this->parseSqlTable($key);
$item[] = $this->parseKey($table) . ' ' . $this->parseKey($options['alias'][$table]); $item[] = $this->parseKey($key) . ' ' . $this->parseKey($table);
} else { } else {
$item[] = $this->parseKey($table); $table = $this->parseSqlTable($table);
if (isset($options['alias'][$table])) {
$item[] = $this->parseKey($table) . ' ' . $this->parseKey($options['alias'][$table]);
} else {
$item[] = $this->parseKey($table);
}
} }
} }
return implode(',', $item); return implode(',', $item);

View File

@@ -671,6 +671,7 @@ class Query
$table = $key; $table = $key;
$alias = array_shift($join); $alias = array_shift($join);
$this->alias([$table => $alias]); $this->alias([$table => $alias]);
$table = [$table => $alias];
} else { } else {
$table = array_shift($join); $table = array_shift($join);
} }
@@ -694,6 +695,7 @@ class Query
if (strpos($table, ' ')) { if (strpos($table, ' ')) {
list($table, $alias) = explode(' ', $table); list($table, $alias) = explode(' ', $table);
$this->alias([$table => $alias]); $this->alias([$table => $alias]);
$table = [$table => $alias];
} }
} }
$this->options['join'][] = [$table, strtoupper($type), $condition]; $this->options['join'][] = [$table, strtoupper($type), $condition];
@@ -1054,7 +1056,7 @@ class Query
$table[] = $val; $table[] = $val;
} else { } else {
$this->alias([$key => $val]); $this->alias([$key => $val]);
$table[] = $key; $table[$key] = $val;
} }
} }
} }
@@ -1365,7 +1367,7 @@ class Query
/** /**
* 获取数据表信息 * 获取数据表信息
* @access public * @access public
* @param string $tableName 数据表名 留空自动获取 * @param mixed $tableName 数据表名 留空自动获取
* @param string $fetch 获取信息类型 包括 fields type bind pk * @param string $fetch 获取信息类型 包括 fields type bind pk
* @return mixed * @return mixed
*/ */
@@ -1575,7 +1577,7 @@ class Query
$name = Loader::parseName(basename(str_replace('\\', '/', $currentModel))); $name = Loader::parseName(basename(str_replace('\\', '/', $currentModel)));
$table = $this->getTable(); $table = $this->getTable();
$alias = isset($info['alias'][$name]) ? $info['alias'][$name] : $name; $alias = isset($info['alias'][$name]) ? $info['alias'][$name] : $name;
$this->table($table)->alias($alias); $this->table([$table => $alias]);
if (isset($this->options['field'])) { if (isset($this->options['field'])) {
$field = $this->options['field']; $field = $this->options['field'];
unset($this->options['field']); unset($this->options['field']);
@@ -1587,7 +1589,7 @@ class Query
// 预载入封装 // 预载入封装
$joinTable = $model->getTable(); $joinTable = $model->getTable();
$joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model']))); $joinName = Loader::parseName(basename(str_replace('\\', '/', $info['model'])));
$joinAlias = isset($info['alias'][$joinName]) ? $info['alias'][$joinName] : $joinName; $joinAlias = isset($info['alias'][$joinName]) ? $info['alias'][$joinName] : $relation;
$this->via($joinAlias); $this->via($joinAlias);
if (Relation::HAS_ONE == $info['type']) { if (Relation::HAS_ONE == $info['type']) {
@@ -1670,8 +1672,9 @@ class Query
{ {
$pk = $this->getPk($options); $pk = $this->getPk($options);
// 获取当前数据表 // 获取当前数据表
if (!empty($options['alias'][$options['table']])) { $table = is_array($options['table']) ? key($options['table']) : $options['table'];
$alias = $options['alias'][$options['table']]; if (!empty($options['alias'][$table])) {
$alias = $options['alias'][$table];
} }
if (is_string($pk)) { if (is_string($pk)) {
$key = isset($alias) ? $alias . '.' . $pk : $pk; $key = isset($alias) ? $alias . '.' . $pk : $pk;
@@ -1983,7 +1986,7 @@ class Query
// 判断查询缓存 // 判断查询缓存
$cache = $options['cache']; $cache = $options['cache'];
if (true === $cache['key'] && !is_null($data) && !is_array($data)) { if (true === $cache['key'] && !is_null($data) && !is_array($data)) {
$key = 'think:' . $options['table'] . '|' . $data; $key = 'think:' . (is_array($options['table']) ? key($options['table']) : $options['table']) . '|' . $data;
} else { } else {
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options)); $key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
} }
@@ -2053,7 +2056,8 @@ class Query
if (!empty($this->model)) { if (!empty($this->model)) {
throw new ModelNotFoundException('model data Not Found:' . $this->model, $this->model, $options); throw new ModelNotFoundException('model data Not Found:' . $this->model, $this->model, $options);
} else { } else {
throw new DataNotFoundException('table data not Found:' . $options['table'], $options['table'], $options); $table = is_array($options['table']) ? key($options['table']) : $options['table'];
throw new DataNotFoundException('table data not Found:' . $table, $table, $options);
} }
} }
@@ -2095,8 +2099,13 @@ class Query
*/ */
public function chunk($count, $callback, $column = null) public function chunk($count, $callback, $column = null)
{ {
$options = $this->getOptions(); $options = $this->getOptions();
$column = $column ?: $this->getPk(isset($options['table']) ? $options['table'] : ''); if (isset($options['table'])) {
$table = is_array($options['table']) ? key($options['table']) : $options['table'];
} else {
$table = '';
}
$column = $column ?: $this->getPk($table);
$bind = $this->bind; $bind = $this->bind;
$resultSet = $this->limit($count)->order($column, 'asc')->select(); $resultSet = $this->limit($count)->order($column, 'asc')->select();