@param BaseQueryRelateContract $queryRelate
@param bool                    $unionAll

@return BaseQueryRelateContract
176135
public function union(BaseQueryRelateContract $queryRelate, bool $unionAll = true): BaseQueryRelateContract
    {
        $this->query = $this->query->union($queryRelate->getQuery(), $unionAll);

        return $this;
    }
846557
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        return $query->select($columns)->whereColumn(
            $this->getQualifiedForeignKeyName(), '=', $query->qualifyColumn($this->ownerKey)
        );
    }
37289
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
    {
        if (strpos($relation, '.') !== false) {
            return $this->hasNested($relation, $operator, $count, $boolean, $callback);
        }

        $relation = $this->getHasRelationQuery($relation);

        $query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);

        if ($callback) call_user_func($callback, $query);

        return $this->addHasWhere($query, $relation, $operator, $count, $boolean);
    }
1113277
public function related($relation, $column, $operator = null, $value = null, $boolean = 'and')
    {
        if ($column instanceof \Closure) {
            return $this->addRelated($relation, $column);
        }

        // If there is no value it is a where = ? query and we set the appropriate params
        if ($value === null) {
            $value = $operator;
            $operator = '=';
        }

        return $this->addRelated($relation, function ($query) use ($column, $operator, $value, $boolean) {
            return $query->where($column, $operator, $value, $boolean);
        });
    }
523167
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
	{
		if ($parent->getQuery()->from == $query->getQuery()->from) {
			return $this->getRelationQueryForSelfJoin($query, $parent, $columns);
		}

		$this->setJoin($query);

		return parent::getRelationQuery($query, $parent, $columns);
	}
955311
public function apply($queryBuilder, JoinMap $joinMap): ?string
    {
        $alias = null;

        $rootAlias = $joinMap->getAlias();
        $rootClassMetadata = $joinMap->getClassMetadata();
        $executedAliases = $this->executedAliases($queryBuilder);

        $alias = $rootAlias;
        foreach ($joinMap->getJoinMap() as $path => $fields) {
            foreach ($fields as $fieldData) {
                $field    = $fieldData['field'];
                $joinType = $fieldData['joinType'];
                $joinAlias = $alias . '_' . $field;

                if (!in_array($joinAlias, $executedAliases)) {
                    if ($path == $rootAlias) {
                        $classMetadata = $rootClassMetadata;

                    } else {
                        $pathWithoutRootAlias = substr($path, strlen($rootAlias) + 1);
                        $classMetadata = $this->getClassMetadataByPath($pathWithoutRootAlias, $rootClassMetadata);
                    }

                    $joinMethod = $joinType == JoinMap::JOIN_TYPE_LEFT ? 'leftJoin' : ($joinType == JoinMap::JOIN_TYPE_INNER ? 'innerJoin' : null);

                    if (!$joinMethod) {
                        throw new \RuntimeException('Join type not defined or has wrong type.');
                    }

                    $associationMapping = $classMetadata->getAssociationMapping($field);

                    if ($associationMapping['type'] === ClassMetadata::MANY_TO_MANY) {
                        // If is owning side
                        if ($associationMapping['isOwningSide']) {
                            $joinTable = $associationMapping['joinTable'];
                            $joinTableName = $joinTable['name'];
                            $joinTableAlias = $alias . '_' . $joinTable['name'];

                            $relationToSourceKeyColumns = $associationMapping['relationToSourceKeyColumns'];

                            $queryBuilder->$joinMethod(
                                $alias,
                                $joinTableName,
                                $joinTableAlias,
                                $joinTableAlias . '.' . key($relationToSourceKeyColumns) . ' = ' . $alias . '.' . current($relationToSourceKeyColumns)
                            );

                            $fieldClassMetadata = $this->getClassMetadata($associationMapping['targetEntity']);
                            $fieldTableName = $fieldClassMetadata->getTableName();
                            $relationToTargetKeyColumns = $associationMapping['relationToTargetKeyColumns'];

                            $queryBuilder->$joinMethod(
                                $alias,
                                $fieldTableName,
                                $joinAlias,
                                $joinAlias . '.' . current($relationToTargetKeyColumns) . ' = ' . $joinTableAlias . '.' . key($relationToTargetKeyColumns)
                            );

                        } else {
                            $fieldClassMetadata = $this->getClassMetadata($associationMapping['targetEntity']);
                            $fieldAssociationMapping = $fieldClassMetadata->getAssociationMapping($associationMapping['mappedBy']);
                            $fieldTableName = $fieldClassMetadata->getTableName();

                            $joinTable = $fieldAssociationMapping['joinTable'];
                            $joinTableName = $joinTable['name'];
                            $joinTableAlias = $alias . '_' . $joinTable['name'];
                            $relationToSourceKeyColumns = $fieldAssociationMapping['relationToSourceKeyColumns'];
                            $relationToTargetKeyColumns = $fieldAssociationMapping['relationToTargetKeyColumns'];

                            $queryBuilder->$joinMethod(
                                $alias,
                                $joinTableName,
                                $joinTableAlias,
                                $joinTableAlias . '.' . key($relationToTargetKeyColumns) . ' = ' . $alias . '.' . current($relationToTargetKeyColumns)
                            );

                            $queryBuilder->$joinMethod(
                                $alias,
                                $fieldTableName,
                                $joinAlias,
                                $joinAlias . '.' . current($relationToSourceKeyColumns) . ' = ' . $joinTableAlias . '.' . key($relationToSourceKeyColumns)
                            );
                        }

                    } elseif ($associationMapping['type'] === ClassMetadata::ONE_TO_MANY) {
                        $fieldClassMetadata = $this->getClassMetadata($associationMapping['targetEntity']);
                        $fieldAssociationMapping = $fieldClassMetadata->getAssociationMapping($associationMapping['mappedBy']);
                        $fieldTableName = $fieldClassMetadata->getTableName();
                        $sourceToTargetKeyColumns = $fieldAssociationMapping['sourceToTargetKeyColumns'];

                        $queryBuilder->$joinMethod(
                            $alias,
                            $fieldTableName,
                            $joinAlias,
                            $joinAlias . '.' . key($sourceToTargetKeyColumns) . ' = ' . $alias . '.' . current($sourceToTargetKeyColumns)
                        );

                    } elseif ($associationMapping['type'] === ClassMetadata::MANY_TO_ONE) {
                        $fieldClassMetadata = $this->getClassMetadata($associationMapping['targetEntity']);
                        $fieldTableName = $fieldClassMetadata->getTableName();
                        $sourceToTargetKeyColumns = $associationMapping['sourceToTargetKeyColumns'];

                        $queryBuilder->$joinMethod(
                            $alias,
                            $fieldTableName,
                            $joinAlias,
                            $joinAlias . '.' . current($sourceToTargetKeyColumns) . ' = ' . $alias . '.' . key($sourceToTargetKeyColumns)
                        );

                    } elseif ($associationMapping['type'] === ClassMetadata::ONE_TO_ONE) {
                        if ($associationMapping['isOwningSide']) {
                            $fieldClassMetadata = $this->getClassMetadata($associationMapping['targetEntity']);
                            $fieldTableName = $fieldClassMetadata->getTableName();
                            $sourceToTargetKeyColumns = $associationMapping['sourceToTargetKeyColumns'];

                            $queryBuilder->leftJoin(
                                $alias,
                                $fieldTableName,
                                $joinAlias,
                                $alias . '.' . key($sourceToTargetKeyColumns) . ' = ' . $joinAlias . '.' . current($sourceToTargetKeyColumns)
                            );

                        } else {
                            $fieldClassMetadata = $this->getClassMetadata($associationMapping['targetEntity']);
                            $fieldTableName = $fieldClassMetadata->getTableName();
                            $fieldAssociationMapping = $fieldClassMetadata->getAssociationMapping($associationMapping['mappedBy']);
                            $sourceToTargetKeyColumns = $fieldAssociationMapping['sourceToTargetKeyColumns'];

                            $queryBuilder->leftJoin(
                                $alias,
                                $fieldTableName,
                                $joinAlias,
                                $alias . '.' . current($sourceToTargetKeyColumns) . ' = ' . $joinAlias . '.' . key($sourceToTargetKeyColumns)
                            );
                        }
                    }
                }

                $alias = $joinAlias;
            }
        }

        return $alias;
    }
1116668
public static function traverseJoins(string $alias, QueryBuilder $queryBuilder, ManagerRegistry $managerRegistry): \Generator
    {
        $rootAliasMap = self::mapRootAliases($queryBuilder->getRootAliases(), $queryBuilder->getRootEntities());

        $joinParts = $queryBuilder->getDQLPart('join');
        $rootAlias = self::findRootAlias($alias, $queryBuilder);

        $joinAliasMap = self::mapJoinAliases($joinParts[$rootAlias]);

        $aliasMap = array_merge($rootAliasMap, $joinAliasMap);

        $apexEntityClass = null;
        $associationStack = [];
        $aliasStack = [];
        $currentAlias = $alias;

        while (null === $apexEntityClass) {
            if (!isset($aliasMap[$currentAlias])) {
                throw new \LogicException(sprintf('Unknown alias "%s".', $currentAlias));
            }

            if (\is_string($aliasMap[$currentAlias])) {
                $aliasStack[] = $currentAlias;
                $apexEntityClass = $aliasMap[$currentAlias];
            } else {
                [$parentAlias, $association] = $aliasMap[$currentAlias];

                $associationStack[] = $association;
                $aliasStack[] = $currentAlias;
                $currentAlias = $parentAlias;
            }
        }

        $entityClass = $apexEntityClass;

        while (null !== ($alias = array_pop($aliasStack))) {
            $metadata = $managerRegistry
                ->getManagerForClass($entityClass)
                ->getClassMetadata($entityClass);

            $association = array_pop($associationStack);

            yield $alias => [
                $metadata,
                $association,
            ];

            if (null !== $association) {
                $entityClass = $metadata->getAssociationTargetClass($association);
            }
        }
    }
493500
protected function dispatch(array $data, QueryRelate $queryRelate): QueryRelate
    {
        foreach ($data as $key => $item) {
            $item = is_array($item) ? $item : trim($item);
            $method = 'by'.studly_case($key);

            if (method_exists($this, $method)) {
                $queryRelate = call_user_func_array([$this, $method], [$queryRelate, $item]);
            }
        }

        return $queryRelate;
    }
1183423
protected function assignJoins(QueryBuilder $queryBuilder, array $joins = null)
    {
        if (null === $joins) {
            $joins = $this->getJoins();
        }

        foreach ($joins as $entity => $join) {
            switch ($join->getJoinMethod()) {
                case DoctrineJoinDescriptor::JOIN_METHOD_LEFT:
                    $queryBuilder->leftJoin(
                        $join->getJoin() ?: $entity,
                        $this->encodeAlias($entity),
                        $join->getJoinConditionMethod(),
                        $join->getJoinCondition()
                    );
                    break;
                case DoctrineJoinDescriptor::JOIN_METHOD_INNER:
                    $queryBuilder->innerJoin(
                        $join->getJoin() ?: $entity,
                        $this->encodeAlias($entity),
                        $join->getJoinConditionMethod(),
                        $join->getJoinCondition()
                    );
                    break;
            }
        }
    }
182649
public function applyRelationFilters(string $relationName, string $alias, Builder $query)
    {
        foreach ($this->getRelationFilters($relationName) as $filter) {
            $filter->apply($query, $alias);
        }
    }
1144871
public function addContextFilterToQueryBuilder($queryBuilder, $context_relation_name = 'context', $context_name = 'default')
    {
        $context = $this->findContext($context_name);

        $queryBuilder->andWhere(
            $queryBuilder->getRootAliases()[0] . '.' . $context_relation_name
            . '=' .
            $this->addNamedParameter($context_relation_name, $context->getId(), $queryBuilder, null)
        );

        return $queryBuilder;
    }
683554
function with(array $relations, array $where = null, $order = null, $limit = 0, $offset = 0) {
		$qthis = $this->conn->quoteName($this->getName());
		$query = $this->getQueryWriter()->reset();
		$query->write("SELECT", "$qthis.*", "FROM", $qthis);
		foreach ($relations as $relation) {
			if (!($relation instanceof Table\Reference)) {
				$relation = static::resolve($relation)->getRelation($this->getName());
			}
			if ($this->getName() === $relation->foreignTable) {
				$query->write("JOIN", $relation->referencedTable)->write("ON");
				foreach ($relation as $key => $ref) {
					$query->criteria(
						array(
							"{$relation->referencedTable}.{$ref}=" => 
								new QueryExpr("{$relation->foreignTable}.{$key}")
						)
					);
				}
			} else {
				$query->write("JOIN", $relation->foreignTable)->write("ON");
				foreach ($relation as $key => $ref) {
					$query->criteria(
						array(
							"{$relation->referencedTable}.{$ref}=" => 
								new QueryExpr("{$relation->foreignTable}.{$key}")
						)
					);
				}
			}
		}
		if ($where) {
			$query->write("WHERE")->criteria($where);
		}
		if ($order) {
			$query->write("ORDER BY", $order);
		}
		if ($limit) {
			$query->write("LIMIT", $limit);
		}
		if ($offset) {
			$query->write("OFFSET", $offset);
		}
		return $this->execute($query);
	}
1085828
public function getRelationExistenceQuery(Query $query, Query $parentQuery, $columns = ['*']) {
        return $query->select($columns)->whereColumn(
            $this->parent->qualifyColumn($this->parent->getKeyName()), '=', $this->getExistenceCompareKey()
        );
    }
949644
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
    {
        if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns);
        }

        $modelTable = $query->getModel()
            ->getTable();

        return $query->select($columns)
            ->whereColumn(
                $this->getQualifiedForeignKey(), '=', is_array($this->ownerKey) ? //Check for multi-columns relationship
                array_map(function ($k) use ($modelTable) {
                    return $modelTable.'.'.$k;
                }, $this->ownerKey) : $modelTable.'.'.$this->ownerKey
            );
    }
1072398
public function query($queryString, array $queryValues = [])
    {
        if (!is_string($queryString) || empty($queryString)) {
            throw new \InvalidArgumentException(
                __METHOD__.': The specified query is not valid.'
            );
        }

        $this->connect();
        $this->resourceHandle = $this->dbConnection->prepare($queryString);

        try {
            // start transaction
            $this->dbConnection->beginTransaction();

            // execute the query and return a status
            $this->executionStatus = $this->resourceHandle->execute($queryValues ?: null);

            // get last inserted id if present
            $this->lastInsertedId = $this->dbConnection->lastInsertId();

            // finally execute the query
            $this->dbConnection->commit();
        } catch (\PDOException $ex) {
            // If an error occurs, execute rollback
            $this->dbConnection->rollBack();

            // Return execution status to false
            $this->executionStatus = false;
            $this->resourceHandle->closeCursor();

            throw new \RuntimeException(
                __METHOD__.": {$ex->getMessage()}\nqueryString: {$queryString}"
            );
        }

        return $this->resourceHandle;
    }
876841
public function leftJoin(string $table, string $first, string $operator = '=', string $two = ''): BaseQueryRelateContract
    {
        $this->query->leftjoin($table, $first, $operator, $two);

        return $this;
    }
702325
public function getRelationCountQuery(Query $query, Query $parent)
    {
        if ($parent->getQuery()->from == $query->getQuery()->from) {
            return $this->getRelationCountQueryForSelfJoin($query, $parent);
        }

        $this->setJoin($query);

        return parent::getRelationCountQuery($query, $parent);
    }