think\model\Relation::eagerlyResult PHP Метод

eagerlyResult() публичный Метод

预载入关联查询 返回模型对象
public eagerlyResult ( Model $result, string $relation, string $class = '' ) : Model
$result think\Model 数据对象
$relation string 关联名
$class string 数据集对象名 为空表示数组
Результат think\Model
    public function eagerlyResult($result, $relation, $class = '')
    {
        $relations = is_string($relation) ? explode(',', $relation) : $relation;
        foreach ($relations as $key => $relation) {
            $subRelation = '';
            $closure = false;
            if ($relation instanceof \Closure) {
                $closure = $relation;
                $relation = $key;
            }
            if (strpos($relation, '.')) {
                list($relation, $subRelation) = explode('.', $relation);
            }
            // 执行关联方法
            $model = $this->parent->{$relation}();
            $localKey = $this->localKey;
            $foreignKey = $this->foreignKey;
            switch ($this->type) {
                case self::HAS_ONE:
                case self::BELONGS_TO:
                    // 模型关联组装
                    $this->match($this->model, $relation, $result);
                    break;
                case self::HAS_MANY:
                    if (isset($result->{$localKey})) {
                        $data = $this->eagerlyOneToMany($model, [$foreignKey => $result->{$localKey}], $relation, $subRelation, $closure);
                        // 关联数据封装
                        if (!isset($data[$result->{$localKey}])) {
                            $data[$result->{$localKey}] = [];
                        }
                        $result->setAttr($relation, $this->resultSetBuild($data[$result->{$localKey}], $class));
                    }
                    break;
                case self::BELONGS_TO_MANY:
                    $pk = $result->getPk();
                    if (isset($result->{$pk})) {
                        $pk = $result->{$pk};
                        // 查询管理数据
                        $data = $this->eagerlyManyToMany($model, ['pivot.' . $localKey => $pk], $relation, $subRelation);
                        // 关联数据封装
                        if (!isset($data[$pk])) {
                            $data[$pk] = [];
                        }
                        $result->setAttr($relation, $this->resultSetBuild($data[$pk], $class));
                    }
                    break;
            }
        }
        return $result;
    }