Query类的relation方法和with方法一样支持使用闭包进行自定义关联查询

This commit is contained in:
thinkphp
2017-01-16 10:31:03 +08:00
parent ba7232b678
commit d5d5314f2e
8 changed files with 41 additions and 10 deletions

View File

@@ -1281,8 +1281,15 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$relations = explode(',', $relations);
}
foreach ($relations as $relation) {
$this->data[$relation] = $this->$relation()->getRelation();
foreach ($relations as $key => $relation) {
$closure = null;
if ($relation instanceof \Closure) {
// 支持闭包查询过滤关联条件
$closure = $relation;
$relation = $key;
}
$method = Loader::parseName($relation, 1, false);
$this->data[$relation] = $this->$method()->getRelation($closure);
}
return $this;
}