8000 merged branch havvg/2.2 (PR #8240) · unframework/symfony@f478812 · GitHub
[go: up one dir, main page]

Skip to content

Commit f478812

Browse files
committed
merged branch havvg/2.2 (PR symfony#8240)
This PR was merged into the 2.2 branch. Discussion ---------- [Propel1] fix many-to-many Propel1 ModelChoiceList | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT replaces symfony#8223 Commits ------- 25e3abd fix many-to-many Propel1 ModelChoiceList
2 parents 1f26887 + 25e3abd commit f478812

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ public function getValuesForChoices(array $models)
256256
*/
257257
public function getIndicesForChoices(array $models)
258258
{
259+
$indices = array();
260+
259261
if (!$this->loaded) {
260262
// Optimize performance for single-field identifiers. We already
261263
// know that the IDs are used as indices
262264

263265
// Attention: This optimization does not check choices for existence
264266
if ($this->identifierAsIndex) {
265-
$indices = array();
266-
267267
foreach ($models as $model) {
268268
if ($model instanceof $this->class) {
269269
// Make sure to convert to the right format
@@ -277,7 +277,30 @@ public function getIndicesForChoices(array $models)
277277
$this->load();
278278
}
279279

280-
return parent::getIndicesForChoices($models);
280+
/*
281+
* Overwriting default implementation.
282+
*
283+
* The two objects may represent the same entry in the database,
284+
* but if they originated from different queries, there are not the same object within the code.
285+
*
286+
* This happens when using m:n relations with either sides model as data_class of the form.
287+
* The choicelist will retrieve the list of available related models with a different query, resulting in different objects.
288+
*/
289+
$choices = $this->fixChoices($models);
290+
foreach ($this->getChoices() as $i => $choice) {
291+
foreach ($choices as $j => $givenChoice) {
292+
if ($this->getIdentifierValues($choice) === $this->getIdentifierValues($givenChoice)) {
293+
$indices[] = $i;
294+
unset($choices[$j]);
295+
296+
if (0 === count($choices)) {
297+
break 2;
298+
}
299+
}
300+
}
301+
}
302+
303+
return $indices;
281304
}
282305

283306
/**

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,18 @@ public function testGetValuesForChoices()
185185
$this->assertEquals(array(1, 2), $choiceList->getValuesForChoices(array($item1, $item2)));
186186
$this->assertEquals(array(1, 2), $choiceList->getIndicesForChoices(array($item1, $item2)));
187187
}
188+
189+
public function testDifferentEqualObjectsAreChoosen()
190+
{
191+
$item = new Item(1, 'Foo');
192+
$choiceList = new ModelChoiceList(
193+
self::ITEM_CLASS,
194+
'value',
195+
array($item)
196+
);
197+
198+
$choosenItem = new Item(1, 'Foo');
199+
200+
$this->assertEquals(array(1), $choiceList->getIndicesForChoices(array($choosenItem)));
201+
}
188202
}

0 commit comments

Comments
 (0)
0