Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\QueryBuilder;
Expand All @@ -35,8 +36,9 @@ final class EagerLoadingExtension implements QueryCollectionExtensionInterface,
private $maxJoins;
private $forceEager;

public function __construct(PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, int $maxJoins = 30, bool $forceEager = true)
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, int $maxJoins = 30, bool $forceEager = true)
{
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
$this->propertyMetadataFactory = $propertyMetadataFactory;
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->maxJoins = $maxJoins;
Expand Down Expand Up @@ -132,8 +134,8 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
continue;
}

$joinColumns = $mapping['joinColumns'] ?? $mapping['joinTable']['joinColumns'] ?? null;
if (false !== $wasLeftJoin || !isset($joinColumns[0]['nullable']) || false !== $joinColumns[0]['nullable']) {
$isNullable = $mapping['joinColumns'][0]['nullable'] ?? true;
if (false !== $wasLeftJoin || true === $isNullable) {
$method = 'leftJoin';
} else {
$method = 'innerJoin';
Expand All @@ -143,10 +145,39 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
$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);
}
}

private function addSelect(QueryBuilder $queryBuilder, string $entity, string $associationAlias, array $propertyMetadataOptions)
{
$select = [];
$entityManager = $queryBuilder->getEntityManager();
$targetClassMetadata = $entityManager->getClassMetadata($entity);

foreach ($this->propertyNameCollectionFactory->create($entity) as $property) {
$propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);

if (true === $propertyMetadata->isIdentifier()) {
$select[] = $property;
continue;
}

//the field test allows to add methods to a Resource which do not reflect real database fields
if (true === $targetClassMetadata->hasField($property) && true === $propertyMetadata->isReadable()) {
$select[] = $property;
}
}

$queryBuilder->addSelect(sprintf('partial %s.{%s}', $associationAlias, implode(',', $select)));
}

/**
* Gets serializer groups if available, if not it returns the $options array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<!-- Doctrine Query extensions -->

<service id="api_platform.doctrine.orm.query_extension.eager_loading" class="ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\EagerLoadingExtension" public="false">
<argument type="service" id="api_platform.metadata.property.name_collection_factory" />
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument>%api_platform.eager_loading.max_joins%</argument>
Expand Down
Loading