Skip to content

Commit

Permalink
Implemented marshalling with fieldList on merging _joinData
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 12, 2014
1 parent 69fbcc4 commit 84a1d32
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ORM/Marshaller.php
Expand Up @@ -402,8 +402,8 @@ protected function _mergeBelongsToMany($original, $assoc, $value, $options) {
$marshaller = $joint->marshaller();

$nested = [];
if (isset($associated['_joinData']['associated'])) {
$nested = ['associated' => (array)$associated['_joinData']['associated']];
if (isset($associated['_joinData'])) {
$nested = (array)$associated['_joinData'];
}

$records = $this->mergeMany($original, $value, $options);
Expand Down
63 changes: 63 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -1211,4 +1211,67 @@ public function testJoinDataWhiteList() {
$this->assertNull($result->tags[1]->_joinData->crazy);
}

/**
* Test merging the _joinData entity for belongstomany associations
* while passing a whitelist
*
* @return void
*/
public function testMergeJoinDataWithFieldList() {
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'tags' => [
[
'id' => 1,
'tag' => 'news',
'_joinData' => [
'active' => 0
]
],
[
'id' => 2,
'tag' => 'cakephp',
'_joinData' => [
'active' => 0
]
],
],
];

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

$data = [
'title' => 'Haz data',
'tags' => [
['id' => 1, 'tag' => 'Cake', '_joinData' => ['foo' => 'bar', 'crazy' => 'something']],
['tag' => 'new tag', '_joinData' => ['active' => 1, 'foo' => 'baz']]
]
];

$tag1 = $entity->tags[0];
$result = $marshall->merge($entity, $data, [
'associated' => ['Tags._joinData' => ['fieldList' => ['foo']]]
]);
$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->assertSame(
['active' => 0, 'foo' => 'bar'],
$entity->tags[0]->_joinData->toArray()
);
$this->assertSame(
['foo' => 'baz'],
$entity->tags[1]->_joinData->toArray()
);
$this->assertEquals('new tag', $entity->tags[1]->tag);
$this->assertTrue($entity->tags[0]->dirty('_joinData'));
$this->assertTrue($entity->tags[1]->dirty('_joinData'));
}

}

0 comments on commit 84a1d32

Please sign in to comment.