Skip to content
Permalink
Browse files

Adding a new test in TableTest.php to check if the user_id in the com…

…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 Mar 31, 2015
1 parent ae6a1a2 commit 10efcc2117c704ea0e72d1ce48795badd5bcdf2d
Showing with 20 additions and 4 deletions.
  1. +2 −4 src/ORM/Marshaller.php
  2. +18 −0 tests/TestCase/ORM/TableTest.php
@@ -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);
}
@@ -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

0 comments on commit 10efcc2

Please sign in to comment.
You can’t perform that action at this time.