23
23
*
24
24
* @author Bernhard Schussek <[email protected] >
25
25
*/
26
- class EntityChoiceLoader implements ChoiceLoaderInterface
26
+ class DoctrineChoiceLoader implements ChoiceLoaderInterface
27
27
{
28
28
/**
29
29
* @var ChoiceListFactoryInterface
@@ -48,7 +48,7 @@ class EntityChoiceLoader implements ChoiceLoaderInterface
48
48
/**
49
49
* @var null|EntityLoaderInterface
50
50
*/
51
- private $ entityLoader ;
51
+ private $ objectLoader ;
52
52
53
53
/**
54
54
* The identifier field, unless the identifier is composite
@@ -70,20 +70,20 @@ class EntityChoiceLoader implements ChoiceLoaderInterface
70
70
private $ choiceList ;
71
71
72
72
/**
73
- * Returns the value of the identifier field of an entity .
73
+ * Returns the value of the identifier field of an object .
74
74
*
75
- * Doctrine must know about this entity , that is, the entity must already
75
+ * Doctrine must know about this object , that is, the object must already
76
76
* be persisted or added to the identity map before. Otherwise an
77
77
* exception is thrown.
78
78
*
79
- * This method assumes that the entity has a single-column identifier and
79
+ * This method assumes that the object has a single-column identifier and
80
80
* will return a single value instead of an array.
81
81
*
82
- * @param object $object The entity for which to get the identifier
82
+ * @param object $object The object for which to get the identifier
83
83
*
84
84
* @return int|string The identifier value
85
85
*
86
- * @throws RuntimeException If the entity does not exist in Doctrine's identity map
86
+ * @throws RuntimeException If the object does not exist in Doctrine's identity map
87
87
*
88
88
* @internal Should not be accessed by user-land code. This method is public
89
89
* only to be usable as callback.
@@ -106,22 +106,23 @@ public static function getIdValue(ObjectManager $om, ClassMetadata $classMetadat
106
106
* Creates a new choice loader.
107
107
*
108
108
* Optionally, an implementation of {@link EntityLoaderInterface} can be
109
- * passed which optimizes the entity loading for one of the Doctrine
109
+ * passed which optimizes the object loading for one of the Doctrine
110
110
* mapper implementations.
111
111
*
112
112
* @param ChoiceListFactoryInterface $factory The factory for creating
113
113
* the loaded choice list
114
114
* @param ObjectManager $manager The object manager
115
- * @param string $class The entity class name
116
- * @param null|EntityLoaderInterface $entityLoader The entity loader
115
+ * @param string $class The class name of the
116
+ * loaded objects
117
+ * @param null|EntityLoaderInterface $objectLoader The objects loader
117
118
*/
118
- public function __construct (ChoiceListFactoryInterface $ factory , ObjectManager $ manager , $ class , EntityLoaderInterface $ entityLoader = null )
119
+ public function __construct (ChoiceListFactoryInterface $ factory , ObjectManager $ manager , $ class , EntityLoaderInterface $ objectLoader = null )
119
120
{
120
121
$ this ->factory = $ factory ;
121
122
$ this ->manager = $ manager ;
122
123
$ this ->classMetadata = $ manager ->getClassMetadata ($ class );
123
124
$ this ->class = $ this ->classMetadata ->getName ();
124
- $ this ->entityLoader = $ entityLoader ;
125
+ $ this ->objectLoader = $ objectLoader ;
125
126
126
127
$ identifier = $ this ->classMetadata ->getIdentifierFieldNames ();
127
128
@@ -140,51 +141,51 @@ public function loadChoiceList($value = null)
140
141
return $ this ->choiceList ;
141
142
}
142
143
143
- $ entities = $ this ->entityLoader
144
- ? $ this ->entityLoader ->getEntities ()
144
+ $ objects = $ this ->objectLoader
145
+ ? $ this ->objectLoader ->getEntities ()
145
146
: $ this ->manager ->getRepository ($ this ->class )->findAll ();
146
147
147
148
// If the class has a multi-column identifier, we cannot index the
148
- // entities by their IDs
149
+ // objects by their IDs
149
150
if ($ this ->compositeId ) {
150
- $ this ->choiceList = $ this ->factory ->createListFromChoices ($ entities , $ value );
151
+ $ this ->choiceList = $ this ->factory ->createListFromChoices ($ objects , $ value );
151
152
152
153
return $ this ->choiceList ;
153
154
}
154
155
155
- // Index the entities by ID
156
- $ entitiesById = array ();
156
+ // Index the objects by ID
157
+ $ objectsById = array ();
157
158
158
- foreach ($ entities as $ entity ) {
159
- $ id = self ::getIdValue ($ this ->manager , $ this ->classMetadata , $ entity );
160
- $ entitiesById [$ id ] = $ entity ;
159
+ foreach ($ objects as $ object ) {
160
+ $ id = self ::getIdValue ($ this ->manager , $ this ->classMetadata , $ object );
161
+ $ objectsById [$ id ] = $ object ;
161
162
}
162
163
163
- $ this ->choiceList = $ this ->factory ->createListFromChoices ($ entitiesById , $ value );
164
+ $ this ->choiceList = $ this ->factory ->createListFromChoices ($ objectsById , $ value );
164
165
165
166
return $ this ->choiceList ;
166
167
}
167
168
168
169
/**
169
- * Loads the values corresponding to the given entities .
170
+ * Loads the values corresponding to the given objects .
170
171
*
171
172
* The values are returned with the same keys and in the same order as the
172
- * corresponding entities in the given array.
173
+ * corresponding objects in the given array.
173
174
*
174
175
* Optionally, a callable can be passed for generating the choice values.
175
- * The callable receives the entity as first and the array key as the second
176
+ * The callable receives the object as first and the array key as the second
176
177
* argument.
177
178
*
178
- * @param array $entities An array of entities . Non-existing entities
179
- * in this array are ignored
179
+ * @param array $objects An array of objects . Non-existing objects in
180
+ * this array are ignored
180
181
* @param null|callable $value The callable generating the choice values
181
182
*
182
183
* @return string[] An array of choice values
183
184
*/
184
- public function loadValuesForChoices (array $ entities , $ value = null )
185
+ public function loadValuesForChoices (array $ objects , $ value = null )
185
186
{
186
187
// Performance optimization
187
- if (empty ($ entities )) {
188
+ if (empty ($ objects )) {
188
189
return array ();
189
190
}
190
191
@@ -195,71 +196,71 @@ public function loadValuesForChoices(array $entities, $value = null)
195
196
if (!$ this ->choiceList && !$ this ->compositeId ) {
196
197
$ values = array ();
197
198
198
- // Maintain order and indices of the given entities
199
- foreach ($ entities as $ i => $ entity ) {
200
- if ($ entity instanceof $ this ->class ) {
199
+ // Maintain order and indices of the given objects
200
+ foreach ($ objects as $ i => $ object ) {
201
+ if ($ object instanceof $ this ->class ) {
201
202
// Make sure to convert to the right format
202
- $ values [$ i ] = (string ) self ::getIdValue ($ this ->manager , $ this ->classMetadata , $ entity );
203
+ $ values [$ i ] = (string ) self ::getIdValue ($ this ->manager , $ this ->classMetadata , $ object );
203
204
}
204
205
}
205
206
206
207
return $ values ;
207
208
}
208
209
209
- return $ this ->loadChoiceList ($ value )->getValuesForChoices ($ entities );
210
+ return $ this ->loadChoiceList ($ value )->getValuesForChoices ($ objects );
210
211
}
211
212
212
213
/**
213
- * Loads the entities corresponding to the given values.
214
+ * Loads the objects corresponding to the given values.
214
215
*
215
- * The entities are returned with the same keys and in the same order as the
216
+ * The objects are returned with the same keys and in the same order as the
216
217
* corresponding values in the given array.
217
218
*
218
219
* Optionally, a callable can be passed for generating the choice values.
219
- * The callable receives the entity as first and the array key as the second
220
+ * The callable receives the object as first and the array key as the second
220
221
* argument.
221
222
*
222
223
* @param string[] $values An array of choice values. Non-existing
223
224
* values in this array are ignored
224
225
* @param null|callable $value The callable generating the choice values
225
226
*
226
- * @return array An array of entities
227
+ * @return array An array of objects
227
228
*/
228
229
public function loadChoicesForValues (array $ values , $ value = null )
229
230
{
230
231
// Performance optimization
231
232
// Also prevents the generation of "WHERE id IN ()" queries through the
232
- // entity loader. At least with MySQL and on the development machine
233
+ // object loader. At least with MySQL and on the development machine
233
234
// this was tested on, no exception was thrown for such invalid
234
235
// statements, consequently no test fails when this code is removed.
235
236
// https://github.com/symfony/symfony/pull/8981#issuecomment-24230557
236
237
if (empty ($ values )) {
237
238
return array ();
238
239
}
239
240
240
- // Optimize performance in case we have an entity loader and
241
+ // Optimize performance in case we have an object loader and
241
242
// a single-field identifier
242
- if (!$ this ->choiceList && !$ this ->compositeId && $ this ->entityLoader ) {
243
- $ unorderedEntities = $ this ->entityLoader ->getEntitiesByIds ($ this ->idField , $ values );
244
- $ entitiesById = array ();
245
- $ entities = array ();
243
+ if (!$ this ->choiceList && !$ this ->compositeId && $ this ->objectLoader ) {
244
+ $ unorderedObjects = $ this ->objectLoader ->getEntitiesByIds ($ this ->idField , $ values );
245
+ $ objectsById = array ();
246
+ $ objects = array ();
246
247
247
248
// Maintain order and indices from the given $values
248
249
// An alternative approach to the following loop is to add the
249
250
// "INDEX BY" clause to the Doctrine query in the loader,
250
251
// but I'm not sure whether that's doable in a generic fashion.
251
- foreach ($ unorderedEntities as $ entity ) {
252
- $ id = self ::getIdValue ($ this ->manager , $ this ->classMetadata , $ entity );
253
- $ entitiesById [$ id ] = $ entity ;
252
+ foreach ($ unorderedObjects as $ object ) {
253
+ $ id = self ::getIdValue ($ this ->manager , $ this ->classMetadata , $ object );
254
+ $ objectsById [$ id ] = $ object ;
254
255
}
255
256
256
257
foreach ($ values as $ i => $ id ) {
257
- if (isset ($ entitiesById [$ id ])) {
258
- $ entities [$ i ] = $ entitiesById [$ id ];
258
+ if (isset ($ objectsById [$ id ])) {
259
+ $ objects [$ i ] = $ objectsById [$ id ];
259
260
}
260
261
}
261
262
262
- return $ entities ;
263
+ return $ objects ;
263
264
}
264
265
265
266
return $ this ->loadChoiceList ($ value )->getChoicesForValues ($ values );
0 commit comments