/** * Joins relations to eager load. * * @param QueryBuilder $queryBuilder * @param QueryNameGeneratorInterface $queryNameGenerator * @param string $resourceClass * @param bool $forceEager * @param string $parentAlias * @param array $propertyMetadataOptions * @param bool $wasLeftJoin if the relation containing the new one had a left join, we have to force the new one to left join too * @param int $joinCount the number of joins * * @throws RuntimeException when the max number of joins has been reached */ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, bool $forceEager, string $parentAlias, array $propertyMetadataOptions = [], bool $wasLeftJoin = false, int &$joinCount = 0) { if ($joinCount > $this->maxJoins) { throw new RuntimeException('The total number of joined relations has exceeded the specified maximum. Raise the limit if necessary.'); } $entityManager = $queryBuilder->getEntityManager(); $classMetadata = $entityManager->getClassMetadata($resourceClass); foreach ($classMetadata->associationMappings as $association => $mapping) { try { $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $association, $propertyMetadataOptions); } catch (PropertyNotFoundException $propertyNotFoundException) { //skip properties not found continue; } catch (ResourceClassNotFoundException $resourceClassNotFoundException) { //skip associations that are not resource classes continue; } if (false === $forceEager && ClassMetadataInfo::FETCH_EAGER !== $mapping['fetch']) { continue; } if (false === $propertyMetadata->isReadableLink() || false === $propertyMetadata->isReadable()) { continue; } $isNullable = $mapping['joinColumns'][0]['nullable'] ?? true; if (false !== $wasLeftJoin || true === $isNullable) { $method = 'leftJoin'; } else { $method = 'innerJoin'; } $associationAlias = $queryNameGenerator->generateJoinAlias($association); $queryBuilder->{$method}(sprintf('%s.%s', $parentAlias, $association), $associationAlias); ++$joinCount; try { $this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $propertyMetadataOptions); } catch (ResourceClassNotFoundException $resourceClassNotFoundException) { continue; } $this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $associationAlias, $propertyMetadataOptions, $method === 'leftJoin', $joinCount); } }
/** * Adds a join to the queryBuilder if none exists. * * @param QueryBuilder $queryBuilder * @param QueryNameGeneratorInterface $queryNameGenerator * @param string $alias * @param string $association the association field * * @return string the new association alias */ protected function addJoinOnce(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $association) : string { $join = $this->getExistingJoin($queryBuilder, $alias, $association); if (null === $join) { $associationAlias = $queryNameGenerator->generateJoinAlias($association); $queryBuilder->join(sprintf('%s.%s', $alias, $association), $associationAlias); } else { $associationAlias = $join->getAlias(); } return $associationAlias; }