8000 [Bridge][Propel1][Form] Model choice accept custom unique column by cedriclombardot · Pull Request #10513 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Bridge][Propel1][Form] Model choice accept custom unique column #10513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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