8000 [Propel1] fix many-to-many Propel1 ModelChoiceList by havvg · Pull Request #8240 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Propel1] fix many-to-many Propel1 ModelChoiceList #8240

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

Merged
merged 1 commit into from
Jun 10, 2013
Merged
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
29 changes: 26 additions & 3 deletions src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ public function getValuesForChoices(array $models)
*/
public function getIndicesForChoices(array $models)
{
$indices = array();

if (!$this->loaded) {
// Optimize performance for single-field identifiers. We already
// know that the IDs are used as indices

// Attention: This optimization does not check choices for existence
if ($this->identifierAsIndex) {
$indices = array();

foreach ($models as $model) {
if ($model instanceof $this->class) {
// Make sure to convert to the right format
Expand All @@ -277,7 +277,30 @@ public function getIndicesForChoices(array $models)
$this->load();
}

return parent::getIndicesForChoices($models);
/*
* Overwriting default implementation.
*
* The two objects may represent the same entry in the database,
* but if they originated from different queries, there are not the same object within the code.
*
* This happens when using m:n relations with either sides model as data_class of the form.
* The choicelist will retrieve the list of available related models with a different query, resulting in different objects.
*/
$choices = $this->fixChoices($models);
foreach ($this->getChoices() as $i => $choice) {
foreach ($choices as $j => $givenChoice) {
if ($this->getIdentifierValues($choice) === $this->getIdentifierValues($givenChoice)) {
$indices[] = $i;
unset($choices[$j]);

if (0 === count($choices)) {
break 2;
}
}
}
}

return $indices;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,18 @@ public function testGetValuesForChoices()
$this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2)));
$this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2)));
}

public function testDifferentEqualObjectsAreChoosen()
{
$item = new Item(1, 'Foo');
$choiceList = new ModelChoiceList(
self::ITEM_CLASS,
'value',
array($item)
);

$choosenItem = new Item(1, 'Foo');

$this->assertEquals(array(1), $choiceList->getIndicesForChoices(array($choosenItem)));
}
}
0