Skip to content

Commit

Permalink
Fix duplicate items in HABTM associations.
Browse files Browse the repository at this point in the history
Apply patch from 'Kim Biesbjerg'.  Fixes issues where nested
HABTM associations would create duplicate content.

Fixes #2564
Fixes #1598
  • Loading branch information
markstory committed Feb 18, 2012
1 parent ba249ae commit 2097d5a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -1389,14 +1389,16 @@ protected function _mergeAssociation(&$data, &$merge, $association, $type, $self
}
} else {
foreach ($merge as $i => $row) {
$insert = array();
if (count($row) === 1) {
if (empty($data[$association]) || (isset($data[$association]) && !in_array($row[$association], $data[$association]))) {
$data[$association][] = $row[$association];
}
} elseif (!empty($row)) {
$tmp = array_merge($row[$association], $row);
unset($tmp[$association]);
$data[$association][] = $tmp;
$insert = $row[$association];
} elseif (isset($row[$association])) {
$insert = array_merge($row[$association], $row);
unset($insert[$association]);
}

if (empty($data[$association]) || (isset($data[$association]) && !in_array($insert, $data[$association], true))) {
$data[$association][] = $insert;
}
}
}
Expand Down

0 comments on commit 2097d5a

Please sign in to comment.