Skip to content

Commit a0ea84b

Browse files
committed
merged branch havvg/feature/propel1-modelchoice-preferred (PR #6454)
This PR was merged into the 2.1 branch. Commits ------- 05fca6d use preferred_choices in favor of preferred_query 6855cff add preferred_query option to ModelType Discussion ---------- [Propel1] add preferred_choices option to ModelType This enables the ModelChoiceList to use 'preferred_choices' of the parent ChoiceType. Ping @willdurand --------------------------------------------------------------------------- by willdurand at 2012-12-22T09:39:39Z :+1:
2 parents f66625d + 05fca6d commit a0ea84b

File tree

3 files changed

+89
-14
lines changed

3 files changed

+89
-14
lines changed

src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Bridge\Propel1\Form\ChoiceList;
1313

14+
use \ModelCriteria;
1415
use \BaseObject;
1516
use \Persistent;
17+
1618
use Symfony\Component\Form\Exception\FormException;
1719
use Symfony\Component\Form\Exception\StringCastException;
1820
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
@@ -21,6 +23,7 @@
2123
* Widely inspirated by the EntityChoiceList.
2224
*
2325
* @author William Durand <william.durand1@gmail.com>
26+
* @author Toni Uebernickel <tuebernickel@gmail.com>
2427
*/
2528
class 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
}

src/Symfony/Bridge/Propel1/Form/Type/ModelType.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,29 @@
2222
* ModelType class.
2323
*
2424
* @author William Durand <william.durand1@gmail.com>
25+
* @author Toni Uebernickel <tuebernickel@gmail.com>
26+
*
27+
* Example using the preferred_choices option.
28+
*
29+
* <code>
30+
* public function buildForm(FormBuilderInterface $builder, array $options)
31+
* {
32+
* $builder
33+
* ->add('product', 'model', array(
34+
* 'class' => 'Model\Product',
35+
* 'query' => ProductQuery::create()
36+
* ->filterIsActive(true)
37+
* ->useI18nQuery($options['locale'])
38+
* ->orderByName()
39+
* ->endUse()
40+
* ,
41+
* 'preferred_choices' => ProductQuery::create()
42+
* ->filterByIsTopProduct(true)
43+
* ,
44+
* ))
45+
* ;
46+
* }
47+
* </code>
2548
*/
2649
class ModelType extends AbstractType
2750
{
@@ -40,7 +63,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
4063
$options['property'],
4164
$options['choices'],
4265
$options['query'],
43-
$options['group_by']
66+
$options['group_by'],
67+
$options['preferred_choices']
4468
);
4569
};
4670

src/Symfony/Bridge/Propel1/Tests/Form/ChoiceList/ModelChoiceListTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ public function testFlattenedChoices()
7070
$this->assertSame(array(1 => $item1, 2 => $item2), $choiceList->getChoices());
7171
}
7272

73+
public function testFlattenedPreferredChoices()
74+
{
75+
$item1 = new Item(1, 'Foo');
76+
$item2 = new Item(2, 'Bar');
77+
78+
$choiceList = new ModelChoiceList(
79+
self::ITEM_CLASS,
80+
'value',
81+
array(
82+
$item1,
83+
$item2,
84+
),
85+
null,
86+
null,
87+
array(
88+
$item1
89+
)
90+
);
91+
92+
$this->assertSame(array(1 => $item1, 2 => $item2), $choiceList->getChoices());
93+
$this->assertEquals(array(1 => new ChoiceView($item1, '1', 'Foo')), $choiceList->getPreferredViews());
94+
}
95+
7396
public function testNestedChoices()
7497
{
7598
$item1 = new Item(1, 'Foo');

0 commit comments

Comments
 (0)