Skip to content

Commit

Permalink
Adding a new test in TableTest.php to check if the user_id in the com…
Browse files Browse the repository at this point in the history
…ments have been set properly after saving a hasMany with _ids attribute.

Signed-off-by: Luan Hospodarsky <luan.handrade@gmail.com>
  • Loading branch information
mylux committed Apr 2, 2015
1 parent ae6a1a2 commit 10efcc2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/ORM/Marshaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,8 @@ protected function _marshalAssociation($assoc, $value, $options)
if ($assoc->type() === Association::MANY_TO_MANY) {
return $marshaller->_belongsToMany($assoc, $value, (array)$options);
}
if ($assoc->type() === Association::ONE_TO_MANY && array_key_exists('_ids', $value)) {
if (is_array($value['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
if ($assoc->type() === Association::ONE_TO_MANY && array_key_exists('_ids', $value) && is_array($value['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
return $marshaller->many($value, (array)$options);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4058,6 +4058,24 @@ public function testSaveWithClonedEntity()
$this->assertEquals(4, $cloned->id);
}

public function testSaveHasManyWithIds()
{
$data = [
'username' => 'lux',
'password' => 'passphrase',
'comments' => [
'_ids' => [1, 2]
]
];

$userTable = TableRegistry::get('Users');
$userTable->hasMany('Comments');
$savedUser = $userTable->save($userTable->newEntity($data, ['associated' => ['Comments']]));
$retrievedUser = $userTable->find('all')->where(['id' => $savedUser->id])->contain(['Comments'])->first();
$this->assertEquals($savedUser->comments[0]->user_id, $retrievedUser->comments[0]->user_id);
$this->assertEquals($savedUser->comments[1]->user_id, $retrievedUser->comments[1]->user_id);
}

/**
* Tests that after saving then entity contains the right primary
* key casted to the right type
Expand Down

0 comments on commit 10efcc2

Please sign in to comment.