Skip to content

Commit

Permalink
Implemented marshalling with fieldList for _joinData
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 12, 2014
1 parent 86c10b3 commit 69fbcc4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/ORM/Marshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
$jointMarshaller = $joint->marshaller();

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

foreach ($records as $i => $record) {
Expand Down
60 changes: 59 additions & 1 deletion tests/TestCase/ORM/MarshallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ public function testAssociatoinsFieldList() {
}

/**
* Tests merging data into an associated entity
* Tests merging associated data with a fieldList
*
* @return void
*/
Expand Down Expand Up @@ -1153,4 +1153,62 @@ public function testMergeAssociationWithfieldList() {
$this->assertTrue($entity->dirty('user'));
}

/**
* Test marshalling nested associations on the _joinData structure
* while having a fieldList
*
* @return void
*/
public function testJoinDataWhiteList() {
$data = [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'tags' => [
[
'tag' => 'news',
'_joinData' => [
'active' => 1,
'crazy' => 'data',
'user' => ['username' => 'Bill'],
]
],
[
'tag' => 'cakephp',
'_joinData' => [
'active' => 0,
'crazy' => 'stuff',
'user' => ['username' => 'Mark'],
]
],
],
];

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

$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, [
'associated' => [
'Tags._joinData' => ['fieldList' => ['active', 'user']],
'Tags._joinData.Users'
]
]);
$this->assertInstanceOf(
'Cake\ORM\Entity',
$result->tags[0]->_joinData->user,
'joinData should contain a user entity.'
);
$this->assertEquals('Bill', $result->tags[0]->_joinData->user->username);
$this->assertInstanceOf(
'Cake\ORM\Entity',
$result->tags[1]->_joinData->user,
'joinData should contain a user entity.'
);
$this->assertEquals('Mark', $result->tags[1]->_joinData->user->username);

$this->assertNull($result->tags[0]->_joinData->crazy);
$this->assertNull($result->tags[1]->_joinData->crazy);
}

}

0 comments on commit 69fbcc4

Please sign in to comment.