Content-Length: 491622 | pFad | http://github.com/symfony/symfony/commit/ec6d2254a75963041e7039c7e02ea85d037bff47

EB [DoctrineBridge] Map entities by single-key when disabling auto-mapping · symfony/symfony@ec6d225 · GitHub
Skip to content

Commit ec6d225

Browse files
[DoctrineBridge] Map entities by single-key when disabling auto-mapping
1 parent 069af99 commit ec6d225

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,13 @@ private function getIdentifier(Request $request, MapEntity $options, string $nam
152152

153153
private function getCriteria(Request $request, MapEntity $options, ObjectManager $manager): array
154154
{
155-
if (null === $mapping = $options->mapping) {
155+
$singleKey = [] === $options->mapping;
156+
157+
if (!$mapping = $options->mapping) {
156158
$mapping = $request->attributes->keys();
157159
}
158160

159-
if ($mapping && \is_array($mapping) && array_is_list($mapping)) {
161+
if ($mapping && array_is_list($mapping)) {
160162
$mapping = array_combine($mapping, $mapping);
161163
}
162164

@@ -176,13 +178,28 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
176178

177179
$criteria = [];
178180
$metadata = $manager->getClassMetadata($options->class);
181+
$usedAttributes = null;
182+
183+
if ($singleKey) {
184+
$usedAttributes ??= $request->attributes->get('_entity_used_attributes') ?? [];
185+
186+
foreach ($usedAttributes as $attribute) {
187+
unset($mapping[$attribute]);
188+
}
189+
}
179190

180191
foreach ($mapping as $attribute => $field) {
181192
if (!$metadata->hasField($field) && (!$metadata->hasAssociation($field) || !$metadata->isSingleValuedAssociation($field))) {
182193
continue;
183194
}
184195

185196
$criteria[$field] = $request->attributes->get($attribute);
197+
198+
if ($singleKey) {
199+
$usedAttributes[] = $attribute;
200+
$request->attributes->set('_entity_used_attributes', $usedAttributes);
201+
break;
202+
}
186203
}
187204

188205
if ($options->stripNull) {

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Map entities by single-key when disabling auto-mapping
8+
49
7.0
510
---
611

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,60 @@ public function testResolveWithMappingAndExclude()
257257
$this->assertSame([$object], $resolver->resolve($request, $argument));
258258
}
259259

260+
public function testResolveWithMultipleEntitiesAndNoAutoMapping()
261+
{
262+
$manager = $this->getMockBuilder(ObjectManager::class)->getMock();
263+
$registry = $this->createRegistry($manager);
264+
$resolver = new EntityValueResolver($registry);
265+
266+
$request = new Request();
267+
$request->attributes->set('foo', 1);
268+
$request->attributes->set('bar', 2);
269+
270+
$argument1 = $this->createArgument('Foo', new MapEntity('Foo', mapping: []));
271+
$argument2 = $this->createArgument('Bar', new MapEntity('Bar', mapping: []));
272+
273+
$metadata1 = $this->getMockBuilder(ClassMetadata::class)->getMock();
274+
$metadata1->expects($this->once())
275+
->method('hasField')
276+
->with('foo')
277+
->willReturn(true);
278+
279+
$metadata2 = $this->getMockBuilder(ClassMetadata::class)->getMock();
280+
$metadata2->expects($this->once())
281+
->method('hasField')
282+
->with('bar')
283+
->willReturn(true);
284+
285+
$manager->expects($this->any())
286+
->method('getClassMetadata')
287+
->willReturnCallback(static fn ($v) => match ($v) {
288+
'Foo' => $metadata1,
289+
'Bar' => $metadata2,
290+
});
291+
292+
$object1 = new \stdClass();
293+
$object2 = new \stdClass();
294+
295+
$repository = $this->getMockBuilder(ObjectRepository::class)->getMock();
296+
$repository->expects($this->any())
297+
->method('findOneBy')
298+
->willReturnCallback(static fn ($v) => match ($v) {
299+
['foo' => 1] => $object1,
300+
['bar' => 2] => $object2,
301+
});
302+
303+
$manager->expects($this->any())
304+
->method('getRepository')
305+
->willReturn($repository);
306+
307+
$this->assertSame([$object1], $resolver->resolve($request, $argument1));
308+
$this->assertSame(['foo'], $request->attributes->get('_entity_used_attributes'));
309+
310+
$this->assertSame([$object2], $resolver->resolve($request, $argument2));
311+
$this->assertSame(['foo', 'bar'], $request->attributes->get('_entity_used_attributes'));
312+
}
313+
260314
public function testExceptionWithExpressionIfNoLanguageAvailable()
261315
{
262316
$manager = $this->getMockBuilder(ObjectManager::class)->getMock();

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/symfony/symfony/commit/ec6d2254a75963041e7039c7e02ea85d037bff47

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy