Skip to content

Commit

Permalink
Model choice accept custom unique column
Browse files Browse the repository at this point in the history
Use now ->add('my_field', 'model', array('class' => 'MyClassWithSlug', 'index_property' => 'slug'));
To make choice list keys as slug
  • Loading branch information
cedriclombardot authored and fabpot committed Mar 26, 2014
1 parent 3b95d09 commit 81e94d0
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
30 changes: 22 additions & 8 deletions src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php
Expand Up @@ -71,17 +71,18 @@ class ModelChoiceList extends ObjectChoiceList
*
* @see \Symfony\Bridge\Propel1\Form\Type\ModelType How to use the preferred choices.
*
* @param string $class The FQCN of the model class to be loaded.
* @param string $labelPath A property path pointing to the property used for the choice labels.
* @param array $choices An optional array to use, rather than fetching the models.
* @param ModelCriteria $queryObject The query to use retrieving model data from database.
* @param string $groupPath A property path pointing to the property used to group the choices.
* @param array|ModelCriteria $preferred The preferred items of this choice.
* @param string $class The FQCN of the model class to be loaded.
* @param string $labelPath A property path pointing to the property used for the choice labels.
* @param array $choices An optional array to use, rather than fetching the models.
* @param ModelCriteria $queryObject The query to use retrieving model data from database.
* @param string $groupPath A property path pointing to the property used to group the choices.
* @param array|ModelCriteria $preferred The preferred items of this choice.
* Either an array if $choices is given,
* or a ModelCriteria to be merged with the $queryObject.
* @param string $useAsIdentifier a custome unique column (eg slug) to use instead of primary key
* @param PropertyAccessorInterface $propertyAccessor The reflection graph for reading property paths.
*/
public function __construct($class, $labelPath = null, $choices = null, $queryObject = null, $groupPath = null, $preferred = array(), PropertyAccessorInterface $propertyAccessor = null)
public function __construct($class, $labelPath = null, $choices = null, $queryObject = null, $groupPath = null, $preferred = array(), PropertyAccessorInterface $propertyAccessor = null, $useAsIdentifier = null)
{
$this->class = $class;

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

$this->query = $queryObject ?: $query;
$this->identifier = $this->query->getTableMap()->getPrimaryKeys();
if ($useAsIdentifier) {
$this->identifier = array( $this->query->getTableMap()->getColumn($useAsIdentifier) );
} else {
$this->identifier = $this->query->getTableMap()->getPrimaryKeys();
}

$this->loaded = is_array($choices) || $choices instanceof \Traversable;

if ($preferred instanceof ModelCriteria) {
Expand Down Expand Up @@ -437,6 +443,14 @@ private function getIdentifierValues($model)
return array();
}

if (1 === count($this->identifier) && current($this->identifier) instanceof \ColumnMap) {
$phpName = current($this->identifier)->getPhpName();

if (method_exists($model, 'get'.$phpName)) {
return array($model->{'get'.$phpName}());
}
}

if ($model instanceof Persistent) {
return array($model->getPrimaryKey());
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Bridge/Propel1/Form/Type/ModelType.php
Expand Up @@ -79,7 +79,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$options['query'],
$options['group_by'],
$options['preferred_choices'],
$propertyAccessor
$propertyAccessor,
$options['index_property']
);
};

Expand All @@ -94,6 +95,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'choice_list' => $choiceList,
'group_by' => null,
'by_reference' => false,
'index_property' => null,
));
}

Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Bridge/Propel1/Tests/Fixtures/Column.php
Expand Up @@ -11,16 +11,17 @@

namespace Symfony\Bridge\Propel1\Tests\Fixtures;

class Column
class Column extends \ColumnMap
{
private $name;

private $type;
protected $type;

public function __construct($name, $type)
{
$this->name = $name;
$this->type = $type;
$this->phpName = ucfirst($name);
}

public function getType()
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Bridge/Propel1/Tests/Fixtures/Item.php
Expand Up @@ -23,12 +23,15 @@ class Item implements \Persistent

private $price;

public function __construct($id = null, $value = null, $groupName = null, $price = null)
private $slug;

public function __construct($id = null, $value = null, $groupName = null, $price = null, $slug = null)
{
$this->id = $id;
$this->value = $value;
$this->groupName = $groupName;
$this->price = $price;
$this->slug = $slug;
}

public function getId()
Expand Down Expand Up @@ -56,6 +59,11 @@ public function getPrice()
return $this->price;
}

public function getSlug()
{
return $this->slug;
}

public function getPrimaryKey()
{
return $this->getId();
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Propel1/Tests/Fixtures/ItemQuery.php
Expand Up @@ -18,6 +18,7 @@ class ItemQuery
'value' => \PropelColumnTypes::VARCHAR,
'price' => \PropelColumnTypes::FLOAT,
'is_active' => \PropelColumnTypes::BOOLEAN,
'slug' => \PropelColumnTypes::VARCHAR,
'enabled' => \PropelColumnTypes::BOOLEAN_EMU,
'updated_at' => \PropelColumnTypes::TIMESTAMP,
);
Expand Down
Expand Up @@ -259,4 +259,26 @@ public function testInvalidClass()
{
$choiceList = new ModelChoiceList('Foo\Bar\DoesNotExistClass');
}

public function testCustomIdentifier()
{
$item1 = new Item(1, 'Foo', null, null, 'slug');
$item2 = new Item(2, 'Bar', null, null, 'slug2');

$choiceList = new ModelChoiceList(
self::ITEM_CLASS,
'value',
array(
$item1,
$item2,
),
null,
null,
array(),
null,
'slug'
);

$this->assertSame(array('slug' => $item1, 'slug2' => $item2), $choiceList->getChoices());
}
}

0 comments on commit 81e94d0

Please sign in to comment.