Skip to content

Commit

Permalink
Merge pull request #6255 from rchavik/hasmany-ids-fields
Browse files Browse the repository at this point in the history
Backward compatibility for #6254
  • Loading branch information
lorenzo committed Apr 2, 2015
2 parents 5c4d1d7 + fddc38e commit 639fcc4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/ORM/Marshaller.php
Expand Up @@ -223,6 +223,9 @@ 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) && is_array($value['_ids'])) {
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
return $marshaller->many($value, (array)$options);
}

Expand Down Expand Up @@ -266,7 +269,7 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
$associated = isset($options['associated']) ? $options['associated'] : [];
$hasIds = array_key_exists('_ids', $data);
if ($hasIds && is_array($data['_ids'])) {
return $this->_loadBelongsToMany($assoc, $data['_ids']);
return $this->_loadAssociatedByIds($assoc, $data['_ids']);
}
if ($hasIds) {
return [];
Expand Down Expand Up @@ -313,7 +316,7 @@ protected function _belongsToMany(Association $assoc, array $data, $options = []
* @param array $ids The list of ids to load.
* @return array An array of entities.
*/
protected function _loadBelongsToMany($assoc, $ids)
protected function _loadAssociatedByIds($assoc, $ids)
{
$target = $assoc->target();
$primaryKey = (array)$target->primaryKey();
Expand All @@ -334,6 +337,19 @@ protected function _loadBelongsToMany($assoc, $ids)
return $target->find()->where($filter)->toArray();
}

/**
* Loads a list of belongs to many from ids.
*
* @param Association $assoc The association class for the belongsToMany association.
* @param array $ids The list of ids to load.
* @return array An array of entities.
* @deprecated Use _loadAssociatedByIds()
*/
protected function _loadBelongsToMany($assoc, $ids)
{
return $this->_loadAssociatedByIds($assoc, $ids);
}

/**
* Merges `$data` into `$entity` and recursively does the same for each one of
* the association names passed in `$options`. When merging associations, if an
Expand Down Expand Up @@ -557,7 +573,7 @@ protected function _mergeBelongsToMany($original, $assoc, $value, $options)
$hasIds = array_key_exists('_ids', $value);
$associated = isset($options['associated']) ? $options['associated'] : [];
if ($hasIds && is_array($value['_ids'])) {
return $this->_loadBelongsToMany($assoc, $value['_ids']);
return $this->_loadAssociatedByIds($assoc, $value['_ids']);
}
if ($hasIds) {
return [];
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -557,6 +557,30 @@ public function testOneBelongsToManyJoinDataAssociatedWithIds()
$this->assertEquals($data['tags'][5]['_joinData']['user']['username'], $result->tags[1]->_joinData->user->username);
}

/**
* Test HasMany association with _ids attribute
*
* @return void
*/
public function testHasManyWithIds()
{
$data = [
'username' => 'lux',
'password' => 'passphrase',
'comments' => [
'_ids' => [1, 2]
]
];

$userTable = TableRegistry::get('Users');
$userTable->hasMany('Comments');
$commentTable = TableRegistry::get('Comments');
$user = $userTable->newEntity($data, ['associated' => ['Comments']]);

$this->assertEquals($user->comments[0], $commentTable->get(1));
$this->assertEquals($user->comments[1], $commentTable->get(2));
}

/**
* Test one() with deeper associations.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/ORM/TableTest.php
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 639fcc4

Please sign in to comment.