Skip to content

Commit

Permalink
Fix missing _joinData for sparse belongsToMany data.
Browse files Browse the repository at this point in the history
When belongsToMany data is sparse the _joinData properties would not be
marshalled correctly. By re-indexing the array of data before we marshal
it we can ensure the indexes will match up.

Refs #6100
  • Loading branch information
markstory committed Mar 21, 2015
1 parent f2ee59b commit e68eba1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/ORM/Marshaller.php
Expand Up @@ -271,6 +271,7 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
if ($hasIds) {
return [];
}
$data = array_values($data);

// Accept [ [id => 1], [id = 2] ] style.
$primaryKey = array_flip($assoc->target()->schema()->primaryKey());
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -506,14 +506,14 @@ public function testOneBelongsToManyJoinDataAssociatedWithIds()
'body' => 'My content',
'author_id' => 1,
'tags' => [
[
3 => [
'id' => 1,
'_joinData' => [
'active' => 1,
'user' => ['username' => 'MyLux'],
]
],
[
5 => [
'id' => 2,
'_joinData' => [
'active' => 0,
Expand Down Expand Up @@ -553,8 +553,8 @@ public function testOneBelongsToManyJoinDataAssociatedWithIds()
$this->assertFalse($result->tags[1]->isNew(), 'Should not be new, as id is in db.');
$this->assertEquals($t1->tag, $result->tags[0]->tag);
$this->assertEquals($t2->tag, $result->tags[1]->tag);
$this->assertEquals($data['tags'][0]['_joinData']['user']['username'], $result->tags[0]->_joinData->user->username);
$this->assertEquals($data['tags'][1]['_joinData']['user']['username'], $result->tags[1]->_joinData->user->username);
$this->assertEquals($data['tags'][3]['_joinData']['user']['username'], $result->tags[0]->_joinData->user->username);
$this->assertEquals($data['tags'][5]['_joinData']['user']['username'], $result->tags[1]->_joinData->user->username);
}

/**
Expand Down

0 comments on commit e68eba1

Please sign in to comment.