Skip to content

Commit

Permalink
Fixed merging associations inside _joinData
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 2, 2014
1 parent ecbe408 commit 52e36fe
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ORM/Marshaller.php
Expand Up @@ -244,7 +244,7 @@ public function merge(EntityInterface $entity, array $data, $include = []) {
public function mergeMany($entities, array $data, $include = []) {
$primary = (array)$this->_table->primaryKey();
$indexed = (new Collection($data))->groupBy($primary[0])->toArray();
$new = [$indexed[null]];
$new = isset($indexed[null]) ? [$indexed[null]] : [];
unset($indexed[null]);
$output = [];

Expand Down Expand Up @@ -317,7 +317,7 @@ protected function _mergeBelongsToMany($original, $assoc, $data, $include) {
$hash = spl_object_hash($record);
$data = $record->get('_joinData');
if (isset($extra[$hash])) {
$record->set('_joinData', $marshaller->merge($extra[$hash], (array)$data));
$record->set('_joinData', $marshaller->merge($extra[$hash], (array)$data, $nested));
} else {
$joinData = $marshaller->one($data, $nested);
$record->set('_joinData', $joinData);
Expand Down
76 changes: 76 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -772,4 +772,80 @@ public function testMergeBelongsToManyJoinData() {
$this->assertTrue($entity->tags[1]->dirty('_joinData'));
}

/**
* Test merging associations inside _joinData
*
* @return void
*/
public function testMergeJoinDataAssociations() {
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'tags' => [
[
'id' => 1,
'tag' => 'news',
'_joinData' => [
'active' => 0,
'user' => ['username' => 'Bill']
]
],
[
'id' => 2,
'tag' => 'cakephp',
'_joinData' => [
'active' => 0
]
],
]
];

$articlesTags = TableRegistry::get('ArticlesTags');
$articlesTags->belongsTo('Users');

$options = [
'Tags' => [
'associated' => [
'_joinData' => ['associated' => ['Users']]
]
]
];
$marshall = new Marshaller($this->articles);
$entity = $marshall->one($data, $options);
$entity->accessible('*', true);

$data = [
'title' => 'Haz data',
'tags' => [
[
'id' => 1,
'tag' => 'news',
'_joinData' => [
'foo' => 'bar',
'user' => ['password' => 'secret']
]
],
[
'id' => 2,
'_joinData' => [
'active' => 1,
'foo' => 'baz',
'user' => ['username' => 'ber']
]
]
]
];

$tag1 = $entity->tags[0];
$result = $marshall->merge($entity, $data, $options);
$this->assertEquals($data['title'], $result->title);
$this->assertEquals('My content', $result->body);
$this->assertSame($tag1, $entity->tags[0]);
$this->assertSame($tag1->_joinData, $entity->tags[0]->_joinData);
$this->assertEquals('Bill', $entity->tags[0]->_joinData->user->username);
$this->assertEquals('secret', $entity->tags[0]->_joinData->user->password);
$this->assertEquals('ber', $entity->tags[1]->_joinData->user->username);
}

}

0 comments on commit 52e36fe

Please sign in to comment.