15
15
use Symfony \Component \Form \Exception \FormException ;
16
16
use Symfony \Component \Form \Exception \UnexpectedTypeException ;
17
17
use Symfony \Component \Form \Extension \Core \ChoiceList \ArrayChoiceList ;
18
- use Doctrine \ORM \ EntityManager ;
18
+ use Doctrine \Common \ Persistence \ ObjectManager ;
19
19
use Doctrine \ORM \QueryBuilder ;
20
20
use Doctrine \ORM \NoResultException ;
21
21
@@ -52,7 +52,7 @@ class EntityChoiceList extends ArrayChoiceList
52
52
*
53
53
* @var Doctrine\ORM\QueryBuilder
54
54
*/
55
- private $ queryBuilder ;
55
+ private $ entityLoader ;
56
56
57
57
/**
58
58
* The fields of which the identifier of the underlying class consists
@@ -63,22 +63,18 @@ class EntityChoiceList extends ArrayChoiceList
63
63
*/
64
64
private $ identifier = array ();
65
65
66
- /**
67
- * A cache for \ReflectionProperty instances for the underlying class
68
- *
69
- * This property should only be accessed through getReflProperty().
70
- *
71
- * @var array
72
- */
73
- private $ reflProperties = array ();
74
-
75
66
/**
76
67
* A cache for the UnitOfWork instance of Doctrine
77
68
*
78
69
* @var Doctrine\ORM\UnitOfWork
79
70
*/
80
71
private $ unitOfWork ;
81
72
73
+ /**
74
+ * Property path to access the key value of this choice-list.
75
+ *
76
+ * @var PropertyPath
77
+ */
82
78
private $ propertyPath ;
83
79
84
80
/**
@@ -91,33 +87,20 @@ class EntityChoiceList extends ArrayChoiceList
91
87
/**
92
88
* Constructor.
93
89
*
94
- * @param EntityManager $em An EntityManager instance
90
+ * @param ObjectManager $manager An EntityManager instance
95
91
* @param string $class The class name
96
92
* @param string $property The property name
97
- * @param QueryBuilder|\Closure $queryBuilder An optional query builder
93
+ * @param EntityLoaderInterface $entityLoader An optional query builder
98
94
* @param array|\Closure $choices An array of choices or a function returning an array
95
+ * @param string $groupBy
99
96
*/
100
- public function __construct (EntityManager $ em , $ class , $ property = null , $ queryBuilder = null , $ choices = null , $ groupBy = null )
97
+ public function __construct (ObjectManager $ manager , $ class , $ property = null , EntityLoaderInterface $ entityLoader = null , $ choices = null , $ groupBy = null )
101
98
{
102
- // If a query builder was passed, it must be a closure or QueryBuilder
103
- // instance
104
- if (!(null === $ queryBuilder || $ queryBuilder instanceof QueryBuilder || $ queryBuilder instanceof \Closure)) {
105
- throw new UnexpectedTypeException ($ queryBuilder , 'Doctrine\ORM\QueryBuilder or \Closure ' );
106
- }
107
-
108
- if ($ queryBuilder instanceof \Closure) {
109
- $ queryBuilder = $ queryBuilder ($ em ->getRepository ($ class ));
110
-
111
- if (!$ queryBuilder instanceof QueryBuilder) {
112
- throw new UnexpectedTypeException ($ queryBuilder , 'Doctrine\ORM\QueryBuilder ' );
113
- }
114
- }
115
-
116
- $ this ->em = $ em ;
99
+ $ this ->em = $ manager ;
117
100
$ this ->class = $ class ;
118
- $ this ->queryBuilder = $ queryBuilder ;
119
- $ this ->unitOfWork = $ em ->getUnitOfWork ();
120
- $ this ->identifier = $ em ->getClassMetadata ($ class )->getIdentifierFieldNames ();
101
+ $ this ->entityLoader = $ entityLoader ;
102
+ $ this ->unitOfWork = $ manager ->getUnitOfWork ();
103
+ $ this ->identifier = $ manager ->getClassMetadata ($ class )->getIdentifierFieldNames ();
121
104
$ this ->groupBy = $ groupBy ;
122
105
123
106
// The property option defines, which property (path) is used for
@@ -150,8 +133,8 @@ protected function load()
150
133
151
134
if (is_array ($ this ->choices )) {
152
135
$ entities = $ this ->choices ;
153
- } elseif ( $ qb = $ this ->queryBuilder ) {
154
- $ entities = $ qb -> getQuery ()-> execute ();
136
+ } else if ( $ entityLoader = $ this ->entityLoader ) {
137
+ $ entities = $ entityLoader -> getEntities ();
155
138
} else {
156
139
$ entities = $ this ->em ->getRepository ($ this ->class )->findAll ();
157
140
}
@@ -171,11 +154,11 @@ protected function load()
171
154
private function groupEntities ($ entities , $ groupBy )
172
155
{
173
156
$ grouped = array ();
157
+ $ path = new PropertyPath ($ groupBy );
174
158
175
159
foreach ($ entities as $ entity ) {
176
160
// Get group name from property path
177
161
try {
178
- $ path = new PropertyPath ($ groupBy );
179
162
$ group = (string ) $ path ->getValue ($ entity );
180
163
} catch (UnexpectedTypeException $ e ) {
181
164
// PropertyPath cannot traverse entity
@@ -307,12 +290,9 @@ public function getEntity($key)
307
290
return isset ($ entities [$ key ]) ? $ entities [$ key ] : null ;
308
291
} elseif ($ this ->entities ) {
309
292
return isset ($ this ->entities [$ key ]) ? $ this ->entities [$ key ] : null ;
310
- } elseif ($ qb = $ this ->queryBuilder ) {
311
- // should we clone the builder?
312
- $ alias = $ qb ->getRootAlias ();
313
- $ where = $ qb ->expr ()->eq ($ alias .'. ' .current ($ this ->identifier ), $ key );
314
-
315
- return $ qb ->andWhere ($ where )->getQuery ()->getSingleResult ();
293
+ } else if ($ entityLoader = $ this ->entityLoader ) {
294
+ $ entities = $ entityLoader ->getEntitiesByKeys ($ this ->identifier , array ($ key ));
295
+ return (isset ($ entities [0 ])) ? $ entities [0 ] : false ;
316
296
}
317
297
318
298
return $ this ->em ->find ($ this ->class , $ key );
@@ -321,23 +301,6 @@ public function getEntity($key)
321
301
}
322
302
}
323
303
324
- /**
325
- * Returns the \ReflectionProperty instance for a property of the underlying class.
326
- *
327
- * @param string $property The name of the property
328
- *
329
- * @return \ReflectionProperty The reflection instance
330
- */
331
- private function getReflProperty ($ property )
332
- {
333
- if (!isset ($ this ->reflProperties [$ property ])) {
334
- $ this ->reflProperties [$ property ] = new \ReflectionProperty ($ this ->class , $ property );
335
- $ this ->reflProperties [$ property ]->setAccessible (true );
336
- }
337
-
338
- return $ this ->reflProperties [$ property ];
339
- }
340
-
341
304
/**
342
305
* Returns the values of the identifier fields of an entity.
343
306
*
0 commit comments