Navigation Menu

Skip to content

Commit

Permalink
Don't include association conditions when marshalling.
Browse files Browse the repository at this point in the history
When marshalling belongsToMany associations we should ignore association
conditions as they can easily contain conditions on the joint table, or
create impossible conditions. Instead we should honour the `prop._ids`
data and leave validating associated data up to the developer, as they
may want to display an error on invalid data.

Refs #5073
  • Loading branch information
markstory committed Nov 6, 2014
1 parent 3f46309 commit 5a8a558
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ORM/Marshaller.php
Expand Up @@ -250,7 +250,7 @@ protected function _loadBelongsToMany($assoc, $ids) {
$filter = [$primaryKey[0] . ' IN' => $ids];
}

return $assoc->find()->where($filter)->toArray();
return $target->find()->where($filter)->toArray();
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/ORM/MarshallerTest.php
Expand Up @@ -886,6 +886,37 @@ public function testMergeFromIdsWithAutoAssociation() {
$this->assertCount(3, $result->tags);
}

/**
* Tests that merging data to an entity containing belongsToMany and _ids
* with additional association conditions works.
*
* @return void
*/
public function testMergeBelongsToManyFromIdsWithConditions() {
$this->articles->belongsToMany('Tags', [
'conditions' => ['ArticleTags.article_id' => 1]
]);

$entity = new Entity([
'title' => 'No tags',
'body' => 'Some content here',
'tags' => []
]);

$data = [
'title' => 'Haz moar tags',
'tags' => ['_ids' => [1, 2, 3]]
];
$entity->accessible('*', true);
$marshall = new Marshaller($this->articles);
$result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);

$this->assertCount(3, $result->tags);
$this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
$this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
$this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
}

/**
* Tests that merging data to an entity containing belongsToMany and _ids
* will ignore empty values.
Expand Down

0 comments on commit 5a8a558

Please sign in to comment.