0 follower

Final Class Yiisoft\ActiveRecord\Internal\JoinsWithBuilder

InheritanceYiisoft\ActiveRecord\Internal\JoinsWithBuilder

Builds {@see ActiveQueryInterface} instance joins added with *join* methods.

Method Details

Hide inherited methods

build() public static method

public static void build ( Yiisoft\ActiveRecord\ActiveQueryInterface $query )
$query Yiisoft\ActiveRecord\ActiveQueryInterface
throws \Yiisoft\Definitions\Exception\CircularReferenceException
throws \Yiisoft\Db\Exception\InvalidConfigException
throws \Yiisoft\Definitions\Exception\NotInstantiableException
throws \Yiisoft\Definitions\Exception\InvalidConfigException

                public static function build(ActiveQueryInterface $query): void
{
    $joins = $query->getJoins();
    $query->setJoins([]);
    $model = $query->getModel();
    foreach ($query->getJoinsWith() as $joinWith) {
        self::joinWithRelations($query, $model, $joinWith);
        $query->with($joinWith->getWith());
    }
    /**
     * Remove duplicated joins added by {@see joinWithRelations()} that may be added, for example, when joining a relation
     * and a via relation at the same time.
     */
    $uniqueJoins = [];
    foreach ($query->getJoins() as $join) {
        $uniqueJoins[serialize($join)] = $join;
    }
    $query->setJoins(array_values($uniqueJoins));
    /**
     * @link https://github.com/yiisoft/yii2/issues/16092
     */
    $uniqueJoinsByTableName = [];
    foreach ($query->getJoins() as $join) {
        $tableName = serialize($join[1]);
        if (!array_key_exists($tableName, $uniqueJoinsByTableName)) {
            $uniqueJoinsByTableName[$tableName] = $join;
        }
    }
    $query->setJoins(array_values($uniqueJoinsByTableName));
    if (!empty($joins)) {
        /**
         * Append explicit join to {@see ActiveQueryInterface::joinWith()} {@link https://github.com/yiisoft/yii2/issues/2880}
         */
        $queryJoins = $query->getJoins();
        $query->setJoins(
            empty($queryJoins) ? $joins : array_merge($queryJoins, $joins),
        );
    }
}