Skip to content

Commit

Permalink
Fix un-aliased query in Marshaller
Browse files Browse the repository at this point in the history
The query used to find possibly existing entities should include the
table alias to avoid ambiguous column names.

Refs #8463
  • Loading branch information
markstory committed Mar 15, 2016
1 parent 692b6d3 commit 0421d5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ORM/Marshaller.php
Expand Up @@ -591,7 +591,8 @@ public function mergeMany($entities, array $data, array $options = [])
return count(array_filter($keys, 'strlen')) === count($primary);
})
->reduce(function ($query, $keys) use ($primary) {
return $query->orWhere($query->newExpr()->and_(array_combine($primary, $keys)));
$fields = array_map([$this->_table, 'aliasField'], $primary);
return $query->orWhere($query->newExpr()->and_(array_combine($fields, $keys)));
}, $this->_table->find());

if (!empty($indexed) && count($maybeExistentQuery->clause('where'))) {
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -2150,6 +2150,30 @@ public function testMergeManyCompositeKey()
$this->assertSame($entities[1], $result[1], 'Should retain object');
}

/**
* Test mergeMany() with forced contain to ensure aliases are used in queries.
*
* @return void
*/
public function testMergeManyExistingQueryAliases()
{
$entities = [
new OpenEntity(['id' => 1, 'comment' => 'First post', 'user_id' => 2], ['markClean' => true]),
];

$data = [
['id' => 1, 'comment' => 'Changed 1', 'user_id' => 1],
['id' => 2, 'comment' => 'Changed 2', 'user_id' => 2],
];
$this->comments->eventManager()->on('Model.beforeFind', function ($event, $query) {
return $query->contain(['Articles']);
});
$marshall = new Marshaller($this->comments);
$result = $marshall->mergeMany($entities, $data);

$this->assertSame($entities[0], $result[0]);
}

/**
* Test mergeMany() when the exist check returns nothing.
*
Expand Down

0 comments on commit 0421d5c

Please sign in to comment.