Skip to content

Commit f68c813

Browse files
authored
Merge pull request #859 from soyuka/proposal-circular-eager-loading
Proposal to avoid join recursivity in eager loading
2 parents 4304e00 + 5ba5180 commit f68c813

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

features/main/circular_reference.feature

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Circular references handling
44
I should be able to catch circular references.
55

66
@createSchema
7-
@dropSchema
87
Scenario: Create a circular reference
98
When I add "Content-Type" header equal to "application/ld+json"
109
And I send a "POST" request to "/circular_references" with body:
@@ -33,3 +32,39 @@ Feature: Circular references handling
3332
]
3433
}
3534
"""
35+
36+
@dropSchema
37+
Scenario: Fetch circular reference
38+
When I add "Content-Type" header equal to "application/ld+json"
39+
And I send a "POST" request to "/circular_references" with body:
40+
"""
41+
{}
42+
"""
43+
And I add "Content-Type" header equal to "application/ld+json"
44+
And I send a "PUT" request to "/circular_references/2" with body:
45+
"""
46+
{
47+
"parent": "/circular_references/1"
48+
}
49+
"""
50+
Then the response status code should be 200
51+
And the response should be in JSON
52+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
53+
And the JSON should be equal to:
54+
"""
55+
{
56+
"@context": "/contexts/CircularReference",
57+
"@id": "/circular_references/2",
58+
"@type": "CircularReference",
59+
"parent": {
60+
"@id": "/circular_references/1",
61+
"@type": "CircularReference",
62+
"parent": "/circular_references/1",
63+
"children": [
64+
"/circular_references/1",
65+
"/circular_references/2"
66+
]
67+
},
68+
"children": []
69+
}
70+
"""

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ private function joinRelations(QueryBuilder $queryBuilder, QueryNameGeneratorInt
151151
continue;
152152
}
153153

154+
if ($mapping['targetEntity'] === $resourceClass) {
155+
$queryBuilder->addSelect($associationAlias);
156+
continue;
157+
}
158+
154159
$this->joinRelations($queryBuilder, $queryNameGenerator, $mapping['targetEntity'], $forceEager, $associationAlias, $propertyMetadataOptions, $method === 'leftJoin', $joinCount);
155160
}
156161
}

tests/Fixtures/TestBundle/Entity/CircularReference.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @author Kévin Dunglas <[email protected]>
2323
*
24-
* @ApiResource(attributes={"normalization_context"={"groups": {"circular"}}, "force_eager"=false})
24+
* @ApiResource(attributes={"normalization_context"={"groups": {"circular"}}})
2525
* @ORM\Entity
2626
*/
2727
class CircularReference

0 commit comments

Comments
 (0)