Skip to content
Permalink
Browse files

Fixed merging associations inside _joinData

  • Loading branch information...
lorenzo committed Mar 2, 2014
1 parent ecbe408 commit 52e36fed04ce3fbb615610c5f4186c86300ffe42
Showing with 78 additions and 2 deletions.
  1. +2 −2 src/ORM/Marshaller.php
  2. +76 −0 tests/TestCase/ORM/MarshallerTest.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);
@@ -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.
You can’t perform that action at this time.