Skip to content

Commit 25c6a2d

Browse files
authored
Merge pull request #860 from soyuka/revert-remove-partial-select
Revert "Remove partial select"
2 parents 84fd0d9 + c918495 commit 25c6a2d

File tree

3 files changed

+185
-33
lines changed

3 files changed

+185
-33
lines changed

src/Bridge/Doctrine/Orm/Extension/EagerLoadingExtension.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
1717
use ApiPlatform\Core\Exception\RuntimeException;
1818
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
19+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
1920
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
2021
use Doctrine\ORM\Mapping\ClassMetadataInfo;
2122
use Doctrine\ORM\QueryBuilder;
@@ -35,8 +36,9 @@ final class EagerLoadingExtension implements QueryCollectionExtensionInterface,
3536
private $maxJoins;
3637
private $forceEager;
3738

38-
public function __construct(PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, int $maxJoins = 30, bool $forceEager = true)
39+
public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, int $maxJoins = 30, bool $forceEager = true)
3940
{
41+
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory;
4042
$this->propertyMetadataFactory = $propertyMetadataFactory;
4143
$this->resourceMetadataFactory = $resourceMetadataFactory;
4244
$this->maxJoins = $maxJoins;
@@ -132,8 +134,8 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
132134
continue;
133135
}
134136

135-
$joinColumns = $mapping['joinColumns'] ?? $mapping['joinTable']['joinColumns'] ?? null;
136-
if (false !== $wasLeftJoin || !isset($joinColumns[0]['nullable']) || false !== $joinColumns[0]['nullable']) {
137+
$isNullable = $mapping['joinColumns'][0]['nullable'] ?? true;
138+
if (false !== $wasLeftJoin || true === $isNullable) {
137139
$method = 'leftJoin';
138140
} else {
139141
$method = 'innerJoin';
@@ -143,10 +145,39 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
143145
$queryBuilder->{$method}(sprintf('%s.%s', $parentAlias, $association), $associationAlias);
144146
++$joinCount;
145147

148+
try {
149+
$this->addSelect($queryBuilder, $mapping['targetEntity'], $associationAlias, $propertyMetadataOptions);
150+
} catch (ResourceClassNotFoundException $resourceClassNotFoundException) {
151+
continue;
152+
}
153+
146154
$this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $associationAlias, $propertyMetadataOptions, $method === 'leftJoin', $joinCount);
147155
}
148156
}
149157

158+
private function addSelect(QueryBuilder $queryBuilder, string $entity, string $associationAlias, array $propertyMetadataOptions)
159+
{
160+
$select = [];
161+
$entityManager = $queryBuilder->getEntityManager();
162+
$targetClassMetadata = $entityManager->getClassMetadata($entity);
163+
164+
foreach ($this->propertyNameCollectionFactory->create($entity) as $property) {
165+
$propertyMetadata = $this->propertyMetadataFactory->create($entity, $property, $propertyMetadataOptions);
166+
167+
if (true === $propertyMetadata->isIdentifier()) {
168+
$select[] = $property;
169+
continue;
170+
}
171+
172+
//the field test allows to add methods to a Resource which do not reflect real database fields
173+
if (true === $targetClassMetadata->hasField($property) && true === $propertyMetadata->isReadable()) {
174+
$select[] = $property;
175+
}
176+
}
177+
178+
$queryBuilder->addSelect(sprintf('partial %s.{%s}', $associationAlias, implode(',', $select)));
179+
}
180+
150181
/**
151182
* Gets serializer groups if available, if not it returns the $options array.
152183
*

src/Bridge/Symfony/Bundle/Resources/config/doctrine_orm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<!-- Doctrine Query extensions -->
8989

9090
<service id="api_platform.doctrine.orm.query_extension.eager_loading" class="ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\EagerLoadingExtension" public="false">
91+
<argument type="service" id="api_platform.metadata.property.name_collection_factory" />
9192
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
9293
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
9394
<argument>%api_platform.eager_loading.max_joins%</argument>

0 commit comments

Comments
 (0)