改进关联预载入查询 两个相同模型查询的问题

This commit is contained in:
thinkphp
2016-06-27 23:29:33 +08:00
parent 0295340e4b
commit 9ca4911c44
2 changed files with 5 additions and 6 deletions

View File

@@ -1466,7 +1466,7 @@ class Query
unset($this->options['with_field']); unset($this->options['with_field']);
} }
} }
$this->field($field, false, $joinTable, $joinAlias, $joinName . '__'); $this->field($field, false, $joinTable, $joinAlias, $relation . '__');
$i++; $i++;
} elseif ($closure) { } elseif ($closure) {
$with[$key] = $closure; $with[$key] = $closure;

View File

@@ -302,23 +302,22 @@ class Relation
*/ */
protected function match($model, $relation, &$result) protected function match($model, $relation, &$result)
{ {
$modelName = Loader::parseName(basename(str_replace('\\', '/', $model)));
// 重新组装模型数据 // 重新组装模型数据
foreach ($result->toArray() as $key => $val) { foreach ($result->toArray() as $key => $val) {
if (strpos($key, '__')) { if (strpos($key, '__')) {
list($name, $attr) = explode('__', $key, 2); list($name, $attr) = explode('__', $key, 2);
if ($name == $modelName) { if ($name == $relation) {
$list[$name][$attr] = $val; $list[$name][$attr] = $val;
unset($result->$key); unset($result->$key);
} }
} }
} }
if (!isset($list[$modelName])) { if (!isset($list[$relation])) {
// 设置关联模型属性 // 设置关联模型属性
$list[$modelName] = []; $list[$relation] = [];
} }
$result->setAttr($relation, new $model($list[$modelName])); $result->setAttr($relation, new $model($list[$relation]));
} }
/** /**