1111
1212namespace Symfony \Bridge \Propel1 \Form \ChoiceList ;
1313
14+ use \ModelCriteria ;
1415use \BaseObject ;
1516use \Persistent ;
17+
1618use Symfony \Component \Form \Exception \FormException ;
1719use Symfony \Component \Form \Exception \StringCastException ;
1820use Symfony \Component \Form \Extension \Core \ChoiceList \ObjectChoiceList ;
2123 * Widely inspirated by the EntityChoiceList.
2224 *
2325 * @author William Durand <[email protected] > 26+ * @author Toni Uebernickel <[email protected] > 2427 */
2528class ModelChoiceList extends ObjectChoiceList
2629{
@@ -31,28 +34,44 @@ class ModelChoiceList extends ObjectChoiceList
3134 *
3235 * @var array
3336 */
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 ;
3545
3646 /**
37- * Query
47+ * The query to retrieve the preferred choices for this list.
48+ *
49+ * @var ModelCriteria
3850 */
39- private $ query = null ;
51+ protected $ preferredQuery ;
4052
4153 /**
4254 * Whether the model objects have already been loaded.
4355 *
4456 * @var Boolean
4557 */
46- private $ loaded = false ;
58+ protected $ loaded = false ;
4759
4860 /**
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.
5473 */
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 () )
5675 {
5776 $ this ->class = $ class ;
5877
@@ -63,13 +82,18 @@ public function __construct($class, $labelPath = null, $choices = null, $queryOb
6382 $ this ->query = $ queryObject ?: $ query ;
6483 $ this ->loaded = is_array ($ choices ) || $ choices instanceof \Traversable;
6584
85+ if ($ preferred instanceof ModelCriteria) {
86+ $ this ->preferredQuery = $ preferred ->mergeWith ($ this ->query );
87+ }
88+
6689 if (!$ this ->loaded ) {
6790 // Make sure the constraints of the parent constructor are
6891 // fulfilled
6992 $ choices = array ();
93+ $ preferred = array ();
7094 }
7195
72- parent ::__construct ($ choices , $ labelPath , array () , $ groupPath );
96+ parent ::__construct ($ choices , $ labelPath , $ preferred , $ groupPath );
7397 }
7498
7599 /**
@@ -317,10 +341,14 @@ private function load()
317341 {
318342 $ models = (array ) $ this ->query ->find ();
319343
344+ $ preferred = array ();
345+ if ($ this ->preferredQuery instanceof ModelCriteria) {
346+ $ preferred = (array ) $ this ->preferredQuery ->find ();
347+ }
348+
320349 try {
321350 // 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 );
324352 } catch (StringCastException $ e ) {
325353 throw new StringCastException (str_replace ('argument $labelPath ' , 'option "property" ' , $ e ->getMessage ()), null , $ e );
326354 }
0 commit comments