Skip to content

Commit 24ef8e1

Browse files
committed
bug #57625 [DoctrineBridge] Make EntityValueResolver return null if a composite ID value is null (MatTheCat)
This PR was merged into the 6.4 branch. Discussion ---------- [DoctrineBridge] Make `EntityValueResolver` return `null` if a composite ID value is `null` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | N/A | License | MIT My use-case is the upsert of an entity identified by two values. I have an `update` route with these two values as route parameters, and a `create` route with only one. In that case the second value is `null` but the `EntityValueResolver` will still call the repository’s `find` method, resulting in a `MissingIdentifierField` exception: > The identifier [VALUE] is missing for a query of [ENTITY] This PR makes the `EntityValueResolver` return `null` in this case, like when a scalar ID is `null`. Commits ------- 87f18842a8 [DoctrineBridge] Make `EntityValueResolver` return `null` if a composite ID value is `null`
2 parents 0de9662 + e6e456a commit 24ef8e1

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

ArgumentResolver/EntityValueResolver.php

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ private function find(ObjectManager $manager, Request $request, MapEntity $optio
104104
if (false === $id || null === $id) {
105105
return $id;
106106
}
107+
if (\is_array($id) && \in_array(null, $id, true)) {
108+
return null;
109+
}
107110

108111
if ($options->evictCache && $manager instanceof EntityManagerInterface) {
109112
$cacheProvider = $manager->getCache();

Tests/ArgumentResolver/EntityValueResolverTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ public function testResolveWithNullId()
144144
$this->assertSame([null], $resolver->resolve($request, $argument));
145145
}
146146

147+
public function testResolveWithArrayIdNullValue()
148+
{
149+
$manager = $this->createMock(ObjectManager::class);
150+
$registry = $this->createRegistry($manager);
151+
$resolver = new EntityValueResolver($registry);
152+
153+
$request = new Request();
154+
$request->attributes->set('nullValue', null);
155+
156+
$argument = $this->createArgument(entity: new MapEntity(id: ['nullValue']), isNullable: true,);
157+
158+
$this->assertSame([null], $resolver->resolve($request, $argument));
159+
}
160+
147161
public function testResolveWithConversionFailedException()
148162
{
149163
$manager = $this->getMockBuilder(ObjectManager::class)->getMock();

0 commit comments

Comments
 (0)