From 52e36fed04ce3fbb615610c5f4186c86300ffe42 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 2 Mar 2014 22:51:51 +0100 Subject: [PATCH] Fixed merging associations inside _joinData --- src/ORM/Marshaller.php | 4 +- tests/TestCase/ORM/MarshallerTest.php | 76 +++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/ORM/Marshaller.php b/src/ORM/Marshaller.php index 32e9396fb44..a82e162d3c1 100644 --- a/src/ORM/Marshaller.php +++ b/src/ORM/Marshaller.php @@ -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 = []; @@ -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); diff --git a/tests/TestCase/ORM/MarshallerTest.php b/tests/TestCase/ORM/MarshallerTest.php index 058f699758e..52f2f8eace4 100644 --- a/tests/TestCase/ORM/MarshallerTest.php +++ b/tests/TestCase/ORM/MarshallerTest.php @@ -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); + } + }