Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb367dd

Browse files
committed
[DoctrineBridge] Deprecated using IdReader when optimization is not possible
1 parent b0989aa commit fb367dd

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* changed guessing of DECIMAL to set the `input` option of `NumberType` to string
88
* deprecated not passing an `IdReader` to the `DoctrineChoiceLoader` when query can be optimized with a single id field
9+
* deprecated passing an `IdReader` to the `DoctrineChoiceLoader` when entities have a composite id
910

1011
4.2.0
1112
-----

src/Symfony/Bridge/Doctrine/Form/ChoiceList/DoctrineChoiceLoader.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ class DoctrineChoiceLoader implements ChoiceLoaderInterface
4242
*
4343
* @param ObjectManager $manager The object manager
4444
* @param string $class The class name of the loaded objects
45-
* @param IdReader $idReader The reader for the object IDs
45+
* @param IdReader|null $idReader The reader for the object IDs
4646
* @param EntityLoaderInterface|null $objectLoader The objects loader
4747
*/
4848
public function __construct(ObjectManager $manager, string $class, IdReader $idReader = null, EntityLoaderInterface $objectLoader = null)
4949
{
5050
$classMetadata = $manager->getClassMetadata($class);
5151

52+
if ($idReader && !$idReader->isSingleId()) {
53+
@trigger_error(sprintf('Passing an instance of "%s" with an entity class "%s" that have a composite id is deprecated since 4.3 and will throw an exception in 5.0.', IdReader::class, $class), E_USER_DEPRECATED);
54+
}
55+
5256
if ((5 > \func_num_args() || false !== func_get_arg(4)) && null === $idReader) {
5357
$idReader = new IdReader($manager, $classMetadata);
5458

@@ -93,7 +97,7 @@ public function loadValuesForChoices(array $choices, $value = null)
9397

9498
// Optimize performance for single-field identifiers. We already
9599
// know that the IDs are used as values
96-
$optimize = null === $value || \is_array($value) && $value[0] === $this->idReader;
100+
$optimize = $this->idReader && (null === $value || \is_array($value) && $value[0] === $this->idReader);
97101

98102
// Attention: This optimization does not check choices for existence
99103
if ($optimize && !$this->choiceList && $this->idReader->isSingleId()) {

src/Symfony/Bridge/Doctrine/Tests/Form/ChoiceList/DoctrineChoiceLoaderTest.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ protected function setUp()
8181
$this->idReader = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader')
8282
->disableOriginalConstructor()
8383
->getMock();
84+
$this->idReader->expects($this->any())
85+
->method('isSingleId')
86+
->willReturn(true)
87+
;
88+
8489
$this->objectLoader = $this->getMockBuilder('Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface')->getMock();
8590
$this->obj1 = (object) ['name' => 'A'];
8691
$this->obj2 = (object) ['name' => 'B'];
@@ -151,7 +156,7 @@ public function testLoadValuesForChoices()
151156
$loader = new DoctrineChoiceLoader(
152157
$this->om,
153158
$this->class,
154-
$this->idReader
159+
null
155160
);
156161

157162
$choices = [$this->obj1, $this->obj2, $this->obj3];
@@ -189,10 +194,6 @@ public function testLoadValuesForChoicesDoesNotLoadIfSingleIntId()
189194
$this->idReader
190195
);
191196

192-
$this->idReader->expects($this->any())
193-
->method('isSingleId')
194-
->willReturn(true);
195-
196197
$this->repository->expects($this->never())
197198
->method('findAll');
198199

@@ -470,4 +471,27 @@ public function testLoaderWithoutIdReaderCanBeOptimized()
470471

471472
$this->assertSame([$obj1], $loader->loadChoicesForValues(['1']));
472473
}
474+
475+
/**
476+
* @group legacy
477+
*
478+
* @deprecationMessage Passing an instance of "Symfony\Bridge\Doctrine\Form\ChoiceList\IdReader" with an entity class "stdClass" that have a composite id is deprecated since 4.3 and will throw an exception in 5.0.
479+
*/
480+
public function testPassingIdReaderWithoutSingleIdEntity()
481+
{
482+
$idReader = $this->createMock(IdReader::class);
483+
$idReader->expects($this->once())
484+
->method('isSingleId')
485+
->willReturn(false)
486+
;
487+
488+
$loader = new DoctrineChoiceLoader(
489+
$this->om,
490+
$this->class,
491+
$idReader,
492+
$this->objectLoader
493+
);
494+
495+
$this->assertInstanceOf(DoctrineChoiceLoader::class, $loader);
496+
}
473497
}

0 commit comments

Comments
 (0)