Skip to content

Commit

Permalink
Fix ambiguous field name errors when marshalling.
Browse files Browse the repository at this point in the history
Merge branch 'marshaller-btm-keyfix' into master.

Fixes ambiguous column names when marshalling belongsToMany associations
by id. This is important when associations use TranslateBehavior.

Refs #6866
  • Loading branch information
markstory committed Jun 25, 2015
2 parents 3b7b880 + eb4a7b6 commit 25d6753
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/ORM/Marshaller.php
Expand Up @@ -283,9 +283,9 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
}
$data = array_values($data);

$primaryKey = array_flip($assoc->target()->schema()->primaryKey());
$records = [];
$conditions = [];
$target = $assoc->target();
$primaryKey = array_flip($target->schema()->primaryKey());
$records = $conditions = [];
$primaryCount = count($primaryKey);

foreach ($data as $i => $row) {
Expand All @@ -295,15 +295,17 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
if (array_intersect_key($primaryKey, $row) === $primaryKey) {
$keys = array_intersect_key($row, $primaryKey);
if (count($keys) === $primaryCount) {
$conditions[] = $keys;
foreach ($keys as $key => $value) {
$conditions[][$target->aliasfield($key)] = $value;
}
}
} else {
$records[$i] = $this->one($row, $options);
}
}

if (!empty($conditions)) {
$query = $assoc->target()->find();
$query = $target->find();
$query->andWhere(function ($exp) use ($conditions) {
return $exp->or_($conditions);
});
Expand Down

0 comments on commit 25d6753

Please sign in to comment.