11
11
12
12
namespace Symfony \Bridge \Propel1 \Form \ChoiceList ;
13
13
14
+ use \ModelCriteria ;
14
15
use \BaseObject ;
15
16
use \Persistent ;
17
+
16
18
use Symfony \Component \Form \Exception \FormException ;
17
19
use Symfony \Component \Form \Exception \StringCastException ;
18
20
use Symfony \Component \Form \Extension \Core \ChoiceList \ObjectChoiceList ;
21
23
* Widely inspirated by the EntityChoiceList.
22
24
*
23
25
* @author William Durand <william.durand1@gmail.com>
26
+ * @author Toni Uebernickel <tuebernickel@gmail.com>
24
27
*/
25
28
class ModelChoiceList extends ObjectChoiceList
26
29
{
@@ -31,28 +34,44 @@ class ModelChoiceList extends ObjectChoiceList
31
34
*
32
35
* @var array
33
36
*/
34
- private $ identifier = array ();
37
+ protected $ identifier = array ();
38
+
39
+ /**
40
+ * The query to retrieve the choices of this list.
41
+ *
42
+ * @var ModelCriteria
43
+ */
44
+ protected $ query ;
35
45
36
46
/**
37
- * Query
47
+ * The query to retrieve the preferred choices for this list.
48
+ *
49
+ * @var ModelCriteria
38
50
*/
39
- private $ query = null ;
51
+ protected $ preferredQuery ;
40
52
41
53
/**
42
54
* Whether the model objects have already been loaded.
43
55
*
44
56
* @var Boolean
45
57
*/
46
- private $ loaded = false ;
58
+ protected $ loaded = false ;
47
59
48
60
/**
49
- * @param string $class
50
- * @param string $labelPath
51
- * @param array $choices
52
- * @param \ModelCriteria $queryObject
53
- * @param string $groupPath
61
+ * Constructor.
62
+ *
63
+ * @see Symfony\Bridge\Propel1\Form\Type\ModelType How to use the preferred choices.
64
+ *
65
+ * @param string $class The FQCN of the model class to be loaded.
66
+ * @param string $labelPath A property path pointing to the property used for the choice labels.
67
+ * @param array $choices An optional array to use, rather than fetching the models.
68
+ * @param ModelCriteria $queryObject The query to use retrieving model data from database.
69
+ * @param string $groupPath A property path pointing to the property used to group the choices.
70
+ * @param array|ModelCriteria $preferred The preferred items of this choice.
71
+ * Either an array if $choices is given,
72
+ * or a ModelCriteria to be merged with the $queryObject.
54
73
*/
55
- public function __construct ($ class , $ labelPath = null , $ choices = null , $ queryObject = null , $ groupPath = null )
74
+ public function __construct ($ class , $ labelPath = null , $ choices = null , $ queryObject = null , $ groupPath = null , $ preferred = array () )
56
75
{
57
76
$ this ->class = $ class ;
58
77
@@ -63,13 +82,18 @@ public function __construct($class, $labelPath = null, $choices = null, $queryOb
63
82
$ this ->query = $ queryObject ?: $ query ;
64
83
$ this ->loaded = is_array ($ choices ) || $ choices instanceof \Traversable;
65
84
85
+ if ($ preferred instanceof ModelCriteria) {
86
+ $ this ->preferredQuery = $ preferred ->mergeWith ($ this ->query );
87
+ }
88
+
66
89
if (!$ this ->loaded ) {
67
90
// Make sure the constraints of the parent constructor are
68
91
// fulfilled
69
92
$ choices = array ();
93
+ $ preferred = array ();
70
94
}
71
95
72
- parent ::__construct ($ choices , $ labelPath , array () , $ groupPath );
96
+ parent ::__construct ($ choices , $ labelPath , $ preferred , $ groupPath );
73
97
}
74
98
75
99
/**
@@ -317,10 +341,14 @@ private function load()
317
341
{
318
342
$ models = (array ) $ this ->query ->find ();
319
343
344
+ $ preferred = array ();
345
+ if ($ this ->preferredQuery instanceof ModelCriteria) {
346
+ $ preferred = (array ) $ this ->preferredQuery ->find ();
347
+ }
348
+
320
349
try {
321
350
// The second parameter $labels is ignored by ObjectChoiceList
322
- // The third parameter $preferredChoices is currently not supported
323
- parent ::initialize ($ models , array (), array ());
351
+ parent ::initialize ($ models , array (), $ preferred );
324
352
} catch (StringCastException $ e ) {
325
353
throw new StringCastException (str_replace ('argument $labelPath ' , 'option "property" ' , $ e ->getMessage ()), null , $ e );
326
354
}
0 commit comments