-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] [DoctrineBridge] optimized LazyChoiceList and DoctrineChoiceLoader #18359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Bridge\Doctrine\Form\ChoiceList; | ||
|
||
use Doctrine\Common\Persistence\ObjectManager; | ||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface; | ||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; | ||
|
@@ -60,20 +61,31 @@ class DoctrineChoiceLoader implements ChoiceLoaderInterface | |
* passed which optimizes the object loading for one of the Doctrine | ||
* mapper implementations. | ||
* | ||
* @param ChoiceListFactoryInterface $factory The factory for creating | ||
* the loaded choice list | ||
* @param ObjectManager $manager The object manager | ||
* @param string $class The class name of the | ||
* loaded objects | ||
* @param IdReader $idReader The reader for the object | ||
* IDs. | ||
* @param ChoiceListFactoryInterface $factory The factory for creating | ||
* the loaded choice list | ||
* @param null|EntityLoaderInterface $objectLoader The objects loader | ||
*/ | ||
public function __construct(ChoiceListFactoryInterface $factory, ObjectManager $manager, $class, IdReader $idReader = null, EntityLoaderInterface $objectLoader = null) | ||
public function __construct($manager, $class, $idReader = null, $objectLoader = null, $factory = null) | ||
{ | ||
// BC to be removed and replace with type hints in 4.0 | ||
if ($manager instanceof ChoiceListFactoryInterface) { | ||
@trigger_error(sprintf('Passing a ChoiceListFactoryInterface to %s is deprecated since version 3.1 and will no longer be supported in 4.0. You should either call "%s::loadChoiceList" or override it to return a ChoiceListInterface.', __CLASS__, __CLASS__)); | ||
|
||
// Provide a BC layer since $factory has changed | ||
// form first to last argument as of 3.1 | ||
$this->factory = $manager; | ||
$manager = $class; | ||
$class = $idReader; | ||
$objectLoader = $factory; | ||
} | ||
|
||
$classMetadata = $manager->getClassMetadata($class); | ||
|
||
$this->factory = $factory; | ||
$this->manager = $manager; | ||
$this->class = $classMetadata->getName(); | ||
$this->idReader = $idReader ?: new IdReader($manager, $classMetadata); | ||
|
@@ -93,9 +105,7 @@ public function loadChoiceList($value = null) | |
? $this->objectLoader->getEntities() | ||
: $this->manager->getRepository($this->class)->findAll(); | ||
|
||
$this->choiceList = $this->factory->createListFromChoices($objects, $value); | ||
|
||
return $this->choiceList; | ||
return $this->choiceList = new ArrayChoiceList($objects, $value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is broken, as it means that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw, this also prevents re-using existing choice lists in the caching factory There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the Once this step done the The thing is actually instead of return an I think this final call should be delegated, that remains coherent with the interfaces and has a performance impact. Anyway it does not break the decorators' resolution of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test with the property path added in #17694 is not broken (ref https://github.com/symfony/symfony/pull/17694/files#diff-641188f07a887e51cad48e9d72d69d07R754) |
||
} | ||
|
||
/** | ||
|
@@ -146,7 +156,7 @@ public function loadChoicesForValues(array $values, $value = null) | |
|
||
// Optimize performance in case we have an object loader and | ||
// a single-field identifier | ||
$optimize = null === $value || is_array($value) && $value[0] === $this->idReader; | ||
$optimize = null === $value || is_array($value) && $this->idReader === $value[0]; | ||
|
||
if ($optimize && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) { | ||
$unorderedObjects = $this->objectLoader->getEntitiesByIds($this->idReader->getIdField(), $values); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a BC break. We cannot reorder the arguments of the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I just realized what you are doing below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I'm just feel bad about removing the type hints but @iltar and @stof were suggesting that change for good reasons I guess.