8000 feature #10513 [Bridge][Propel1][Form] Model choice accept custom uni… · symfony/symfony@9dc14a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9dc14a5

Browse files
committed
feature #10513 [Bridge][Propel1][Form] Model choice accept custom unique column (cedriclombardot)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Bridge][Propel1][Form] Model choice accept custom unique column | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT This PR permit to do : ```` php <?php public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('my_field', 'model', array( 'class' => 'MyClassWithSlug', 'index_property' => 'slug' )); } ```` With this option you can hide internal ids to expose an unique column eg slug and render ```` html <select name="my_field"> <option value="{THE_SLUG}">{THE_VALUE}</option> </select> ```` Commits ------- 81e94d0 Model choice accept custom unique column
2 parents 3b95d09 + 81e94d0 commit 9dc14a5

File tree

6 files changed

+60
-12
lines changed

6 files changed

+60
-12
lines changed

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,18 @@ class ModelChoiceList extends ObjectChoiceList
7171
*
7272
* @see \Symfony\Bridge\Propel1\Form\Type\ModelType How to use the preferred choices.
7373
*
74-
* @param string $class The FQCN of the model class to be loaded.
75-
* @param string $labelPath A property path pointing to the property used for the choice labels.
76-
* @param array $choices An optional array to use, rather than fetching the models.
8000
77-
* @param ModelCriteria $queryObject The query to use retrieving model data from database.
78-
* @param string $groupPath A property path pointing to the property used to group the choices.
79-
* @param array|ModelCriteria $preferred The preferred items of this choice.
74+
* @param string $class The FQCN of the model class to be loaded.
75+
* @param string $labelPath A property path pointing to the property used for the choice labels.
76+
* @param array $choices An optional array to use, rather than fetching the models.
77+
* @param ModelCriteria $queryObject The query to use retrieving model data from database.
78+
* @param string $groupPath A property path pointing to the property used to group the choices.
79+
* @param array|ModelCriteria $preferred The preferred items of this choice.
8080
* Either an array if $choices is given,
8181
* or a ModelCriteria to be merged with the $queryObject.
82+
* @param string $useAsIdentifier a custome unique column (eg slug) to use instead of primary key
8283
* @param PropertyAccessorInterface $propertyAccessor The reflection graph for reading property paths.
8384
*/
84-
public function __construct($class, $labelPath = null, $choices = null, $queryObject = null, $groupPath = null, $preferred = array(), PropertyAccessorInterface $propertyAccessor = null)
85+
public function __construct($class, $labelPath = null, $choices = null, $queryObject = null, $groupPath = null, $preferred = array(), PropertyAccessorInterface $propertyAccessor = null, $useAsIdentifier = null)
8586
{
8687
$this->class = $class;
8788

@@ -96,7 +97,12 @@ public function __construct($class, $labelPath = null, $choices = null, $queryOb
9697
$query = new $queryClass();
9798

9899
$this->query = $queryObject ?: $query;
99-
$this->identifier = $this->query->getTableMap()->getPrimaryKeys();
100+
if ($useAsIdentifier) {
101+
$this->identifier = array( $this->query->getTableMap()->getColumn($useAsIdentifier) );
102+
} else {
103+
$this->identifier = $this->query->getTableMap()->getPrimaryKeys();
104+
}
105+
100106
$this->loaded = is_array($choices) || $choices instanceof \Traversable;
101107

102108
if ($preferred instanceof ModelCriteria) {
@@ -437,6 +443,14 @@ private function getIdentifierValues($model)
437443
return array();
438 8000 444
}
439445

446+
if (1 === count($this->identifier) && current($this->identifier) instanceof \ColumnMap) {
447+
$phpName = current($this->identifier)->getPhpName();
448+
449+
if (method_exists($model, 'get'.$phpName)) {
450+
return array($model->{'get'.$phpName}());
451+
}
452+
}
453+
440454
if ($model instanceof Persistent) {
441455
return array($model->getPrimaryKey());
442456
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
7979
$options['query'],
8080
$options['group_by'],
8181
$options['preferred_choices'],
82-
$propertyAccessor
82+
$propertyAccessor,
83+
$options['index_property']
8384
);
8485
};
8586

@@ -94,6 +95,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
9495
'choice_list' => $choiceList,
9596
'group_by' => null,
9697
'by_reference' => false,
98+
'index_property' => null,
9799
));
98100
}
99101

src/Symfony/Bridge/Propel1/Tests/Fixtures/Column.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111

1212
namespace Symfony\Bridge\Propel1\Tests\Fixtures;
1313

14-
class Column
14+
class Column extends \ColumnMap
1515
{
1616
private $name;
1717

18-
private $type;
18+
protected $type;
1919

2020
public function __construct($name, $type)
2121
{
2222
$this->name = $name;
2323
$this->type = $type;
24+
$this->phpName = ucfirst($name);
2425
}
2526

2627
public function getType()

src/Symfony/Bridge/Propel1/Tests/Fixtures/Item.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ class Item implements \Persistent
2323

2424
private $price;
2525

26-
public function __construct($id = null, $value = null, $groupName = null, $price = null)
26+
private $slug;
27+
28+
public function __construct($id = null, $value = null, $groupName = null, $price = null, $slug = null)
2729
{
2830
$this->id = $id;
2931
$this->value = $value;
3032
$this->groupName = $groupName;
3133
$this->price = $price;
34+
$this->slug = $slug;
3235
}
3336

3437
public function getId()
@@ -56,6 +59,11 @@ public function getPrice()
5659
return $this->price;
5760
}
5861

62+
public function getSlug()
63+
{
64+
return $this->slug;
65+
}
66+
5967
public function getPrimaryKey()
6068
{
6169
return $this->getId();

src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ItemQuery
1818
'value' => \PropelColumnTypes::VARCHAR,
1919
'price' => \PropelColumnTypes::FLOAT,
2020
'is_active' => \PropelColumnTypes::BOOLEAN,
21+
'slug' => \PropelColumnTypes::VARCHAR,
2122
'enabled' => \PropelColumnTypes::BOOLEAN_EMU,
2223
'updated_at' => \PropelColumnTypes::TIMESTAMP,
2324
);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,26 @@ public function testInvalidClass()
259259
{
260260
$choiceList = new ModelChoiceList('Foo\Bar\DoesNotExistClass');
261261
}
262+
263+
public function testCustomIdentifier()
264+
{
265+
$item1 = new Item(1, 'Foo', null, null, 'slug');
266+
$item2 = new Item(2, 'Bar', null, null, 'slug2');
267+
268+
$choiceList = new ModelChoiceList(
269+
self::ITEM_CLASS,
270+
'value',
271+
array(
272+
$item1,
273+
$item2,
274+
),
275+
null,
276+
null,
277+
array(),
278+
null,
279+
'slug'
280+
);
281+
282+
$this->assertSame(array('slug' => $item1, 'slug2' => $item2), $choiceList->getChoices());
283+
}
262284
}

0 commit comments

Comments
 (0)
0