1313
1414use Doctrine \Persistence \ObjectManager ;
1515use Symfony \Component \Form \ChoiceList \ArrayChoiceList ;
16- use Symfony \Component \Form \ChoiceList \ChoiceListInterface ;
1716use Symfony \Component \Form \ChoiceList \Loader \ChoiceLoaderInterface ;
1817
1918/**
@@ -29,9 +28,9 @@ class DoctrineChoiceLoader implements ChoiceLoaderInterface
2928 private $ objectLoader ;
3029
3130 /**
32- * @var ChoiceListInterface
31+ * @var array|null
3332 */
34- private $ choiceList ;
33+ private $ choices ;
3534
3635 /**
3736 * Creates a new choice loader.
@@ -74,15 +73,13 @@ public function __construct(ObjectManager $manager, string $class, IdReader $idR
7473 */
7574 public function loadChoiceList ($ value = null )
7675 {
77- if ($ this ->choiceList ) {
78- return $ this ->choiceList ;
76+ if (null === $ this ->choices ) {
77+ $ this ->choices = $ this ->objectLoader
78+ ? $ this ->objectLoader ->getEntities ()
79+ : $ this ->manager ->getRepository ($ this ->class )->findAll ();
7980 }
8081
81- $ objects = $ this ->objectLoader
82- ? $ this ->objectLoader ->getEntities ()
83- : $ this ->manager ->getRepository ($ this ->class )->findAll ();
84-
85- return $ this ->choiceList = new ArrayChoiceList ($ objects , $ value );
82+ return new ArrayChoiceList ($ this ->choices , $ value );
8683 }
8784
8885 /**
@@ -100,7 +97,7 @@ public function loadValuesForChoices(array $choices, $value = null)
10097 $ optimize = $ this ->idReader && (null === $ value || \is_array ($ value ) && $ value [0 ] === $ this ->idReader );
10198
10299 // Attention: This optimization does not check choices for existence
103- if ($ optimize && !$ this ->choiceList && $ this ->idReader ->isSingleId ()) {
100+ if ($ optimize && !$ this ->choices && $ this ->idReader ->isSingleId ()) {
104101 $ values = [];
105102
106103 // Maintain order and indices of the given objects
@@ -136,7 +133,7 @@ public function loadChoicesForValues(array $values, $value = null)
136133 // a single-field identifier
137134 $ optimize = $ this ->idReader && (null === $ value || \is_array ($ value ) && $ this ->idReader === $ value [0 ]);
138135
139- if ($ optimize && !$ this ->choiceList && $ this ->objectLoader && $ this ->idReader ->isSingleId ()) {
136+ if ($ optimize && !$ this ->choices && $ this ->objectLoader && $ this ->idReader ->isSingleId ()) {
140137 $ unorderedObjects = $ this ->objectLoader ->getEntitiesByIds ($ this ->idReader ->getIdField (), $ values );
141138 $ objectsById = [];
142139 $ objects = [];
0 commit comments