Skip to content

Commit 119e7d0

Browse files
bug #47242 [DoctrineBridge] Do not ignore null IDs in EntityValueResolver (MatTheCat)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [DoctrineBridge] Do not ignore null IDs in EntityValueResolver | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #47166 | License | MIT | Doc PR | N/A The `EntityValueResolver` first try to fetch an entity by its ID but handle null and non-existent IDs the same. If a `null` ID is present, it feels weird to ignore it and try matching an entity using other request attributes. Better return `null` in this case. Commits ------- db05d4c [DoctrineBridge] Do not ignore null IDs in EntityValueResolver
2 parents 56fc4b8 + db05d4c commit 119e7d0

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Symfony/Bridge/Doctrine/ArgumentResolver/EntityValueResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
7474
if (null === $object = $this->findViaExpression($class, $request, $options->expr, $options)) {
7575
$errorMessage = sprintf('The expression "%s" returned null', $options->expr);
7676
}
77-
// find by identifier?
77+
// find by identifier?
7878
} elseif (false === $object = $this->find($class, $request, $options, $name)) {
7979
// find by criteria
8080
if (false === $object = $this->findOneBy($class, $request, $options)) {
@@ -123,7 +123,7 @@ private function find(string $class, Request $request, MapEntity $options, strin
123123

124124
$id = $this->getIdentifier($request, $options, $name);
125125
if (false === $id || null === $id) {
126-
return false;
126+
return $id;
127127
}
128128

129129
$objectManager = $this->getManager($options->objectManager, $class);

src/Symfony/Bridge/Doctrine/Tests/ArgumentResolver/EntityValueResolverTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\Bridge\Doctrine\Tests\ArgumentResolver;
413

514
use Doctrine\DBAL\Types\ConversionException;
@@ -214,6 +223,21 @@ public function testApplyWithId(string|int $id)
214223
$this->assertYieldEquals([$object], $ret);
215224
}
216225

226+
public function testApplyWithNullId()
227+
{
228+
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
229+
$converter = new EntityValueResolver($registry);
230+
231+
$request = new Request();
232+
$request->attributes->set('id', null);
233+
234+
$argument = $this->createArgument(isNullable: true);
235+
236+
$ret = $converter->resolve($request, $argument);
237+
238+
$this->assertYieldEquals([null], $ret);
239+
}
240+
217241
public function testApplyWithConversionFailedException()
218242
{
219243
$registry = $this->getMockBuilder(ManagerRegistry::class)->getMock();
@@ -276,7 +300,7 @@ public function testApplyGuessOptional()
276300
$converter = new EntityValueResolver($registry);
277301

278302
$request = new Request();
279-
$request->attributes->set('arg', null);
303+
$request->attributes->set('guess', null);
280304

281305
$argument = $this->createArgument('stdClass', new MapEntity(), 'arg', true);
282306

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy