|
13 | 13 |
|
14 | 14 | use Symfony\Component\Form\Util\PropertyPath;
|
15 | 15 | use Symfony\Component\Form\Exception\FormException;
|
16 |
| -use Symfony\Component\Form\Extension\Core\ChoiceList\ArrayChoiceList; |
| 16 | +use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList; |
17 | 17 |
|
18 | 18 | /**
|
19 | 19 | * Widely inspirated by the EntityChoiceList (Symfony2).
|
20 | 20 | *
|
21 | 21 | * @author William Durand <william.durand1@gmail.com>
|
22 | 22 | */
|
23 |
| -class ModelChoiceList extends ArrayChoiceList |
| 23 | +class ModelChoiceList extends ObjectChoiceList |
24 | 24 | {
|
25 | 25 | /**
|
26 | 26 | * The models from which the user can choose
|
@@ -89,61 +89,6 @@ public function __construct($class, $property = null, $choices = array(), $query
|
89 | 89 | parent::__construct($choices);
|
90 | 90 | }
|
91 | 91 |
|
92 |
| - /** |
93 |
| - * Initializes the choices and returns them |
94 |
| - * |
95 |
| - * The choices are generated from the models. If the models have a |
96 |
| - * composite identifier, the choices are indexed using ascending integers. |
97 |
| - * Otherwise the identifiers are used as indices. |
98 |
| - * |
99 |
| - * If the models were passed in the "choices" option, this method |
100 |
| - * does not have any significant overhead. Otherwise, if a query object |
101 |
| - * was passed in the "query" option, this query is now used and executed. |
102 |
| - * In the last case, all models for the underlying class are fetched. |
103 |
| - * |
104 |
| - * If the option "property" was passed, the property path in that option |
105 |
| - * is used as option values. Otherwise this method tries to convert |
106 |
| - * objects to strings using __toString(). |
107 |
| - * |
108 |
| - * @return array An array of choices |
109 |
| - */ |
110 |
| - protected function load() |
111 |
| - { |
112 |
| - parent::load(); |
113 |
| - |
114 |
| - if ($this->choices) { |
115 |
| - $models = $this->choices; |
116 |
| - } else { |
117 |
| - $models = $this->query->find(); |
118 |
| - } |
119 |
| - |
120 |
| - $this->choices = array(); |
121 |
| - $this->models = array(); |
122 |
| - |
123 |
| - foreach ($models as $key => $model) { |
124 |
| - if ($this->propertyPath) { |
125 |
| - // If the property option was given, use it |
126 |
| - $value = $this->propertyPath->getValue($model); |
127 |
| - } else { |
128 |
| - // Otherwise expect a __toString() method in the model |
129 |
| - $value = (string)$model; |
130 |
| - } |
131 |
| - |
132 |
| - if (count($this->identifier) > 1) { |
133 |
| - // When the identifier consists of multiple field, use |
134 |
| - // naturally ordered keys to refer to the choices |
135 |
| - $this->choices[$key] = $value; |
136 |
| - $this->models[$key] = $model; |
137 |
| - } else { |
138 |
| - // When the identifier is a single field, index choices by |
139 |
| - // model ID for performance reasons |
140 |
| - $id = current($this->getIdentifierValues($model)); |
141 |
| - $this->choices[$id] = $value; |
142 |
| - $this->models[$id] = $model; |
143 |
| - } |
144 |
| - } |
145 |
| - } |
146 |
| - |
147 | 92 | public function getIdentifier()
|
148 | 93 | {
|
149 | 94 | return $this->identifier;
|
@@ -226,4 +171,59 @@ public function getIdentifierValues($model)
|
226 | 171 |
|
227 | 172 | return $model->getPrimaryKeys();
|
228 | 173 | }
|
| 174 | + |
| 175 | + /** |
| 176 | + * Initializes the choices and returns them |
| 177 | + * |
| 178 | + * The choices are generated from the models. If the models have a |
| 179 | + * composite identifier, the choices are indexed using ascending integers. |
| 180 | + * Otherwise the identifiers are used as indices. |
| 181 | + * |
| 182 | + * If the models were passed in the "choices" option, this method |
| 183 | + * does not have any significant overhead. Otherwise, if a query object |
| 184 | + * was passed in the "query" option, this query is now used and executed. |
| 185 | + * In the last case, all models for the underlying class are fetched. |
| 186 | + * |
| 187 | + * If the option "property" was passed, the property path in that option |
| 188 | + * is used as option values. Otherwise this method tries to convert |
| 189 | + * objects to strings using __toString(). |
| 190 | + * |
| 191 | + * @return array An array of choices |
| 192 | + */ |
| 193 | + protected function load() |
| 194 | + { |
| 195 | + parent::load(); |
| 196 | + |
| 197 | + if ($this->choices) { |
| 198 | + $models = $this->choices; |
| 199 | + } else { |
| 200 | + $models = $this->query->find(); |
| 201 | + } |
| 202 | + |
| 203 | + $this->choices = array(); |
| 204 | + $this->models = array(); |
| 205 | + |
| 206 | + foreach ($models as $key => $model) { |
| 207 | + if ($this->propertyPath) { |
| 208 | + // If the property option was given, use it |
| 209 | + $value = $this->propertyPath->getValue($model); |
| 210 | + } else { |
| 211 | + // Otherwise expect a __toString() method in the model |
| 212 | + $value = (string)$model; |
| 213 | + } |
| 214 | + |
| 215 | + if (count($this->identifier) > 1) { |
| 216 | + // When the identifier consists of multiple field, use |
| 217 | + // naturally ordered keys to refer to the choices |
| 218 | + $this->choices[$key] = $value; |
| 219 | + $this->models[$key] = $model; |
| 220 | + } else { |
| 221 | + // When the identifier is a single field, index choices by |
| 222 | + // model ID for performance reasons |
| 223 | + $id = current($this->getIdentifierValues($model)); |
| 224 | + $this->choices[$id] = $value; |
| 225 | + $this->models[$id] = $model; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
229 | 229 | }
|
0 commit comments