yii\db\ActiveQuery::joinWithRelations PHP Method

joinWithRelations() private method

Modifies the current query by adding join fragments based on the given relations.
private joinWithRelations ( ActiveRecord $model, array $with, string | array $joinType )
$model ActiveRecord the primary model
$with array the relations to be joined
$joinType string | array the join type
    private function joinWithRelations($model, $with, $joinType)
    {
        $relations = [];
        foreach ($with as $name => $callback) {
            if (is_int($name)) {
                $name = $callback;
                $callback = null;
            }
            $primaryModel = $model;
            $parent = $this;
            $prefix = '';
            while (($pos = strpos($name, '.')) !== false) {
                $childName = substr($name, $pos + 1);
                $name = substr($name, 0, $pos);
                $fullName = $prefix === '' ? $name : "{$prefix}.{$name}";
                if (!isset($relations[$fullName])) {
                    $relations[$fullName] = $relation = $primaryModel->getRelation($name);
                    $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName));
                } else {
                    $relation = $relations[$fullName];
                }
                $primaryModel = new $relation->modelClass();
                $parent = $relation;
                $prefix = $fullName;
                $name = $childName;
            }
            $fullName = $prefix === '' ? $name : "{$prefix}.{$name}";
            if (!isset($relations[$fullName])) {
                $relations[$fullName] = $relation = $primaryModel->getRelation($name);
                if ($callback !== null) {
                    call_user_func($callback, $relation);
                }
                if (!empty($relation->joinWith)) {
                    $relation->buildJoinWith();
                }
                $this->joinWithRelation($parent, $relation, $this->getJoinType($joinType, $fullName));
            }
        }
    }