Skip to content

Commit 5a8a558

Browse files
committed
Don't include association conditions when marshalling.
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
1 parent 3f46309 commit 5a8a558

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/ORM/Marshaller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected function _loadBelongsToMany($assoc, $ids) {
250250
$filter = [$primaryKey[0] . ' IN' => $ids];
251251
}
252252

253-
return $assoc->find()->where($filter)->toArray();
253+
return $target->find()->where($filter)->toArray();
254254
}
255255

256256
/**

tests/TestCase/ORM/MarshallerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,37 @@ public function testMergeFromIdsWithAutoAssociation() {
886886
$this->assertCount(3, $result->tags);
887887
}
888888

889+
/**
890+
* Tests that merging data to an entity containing belongsToMany and _ids
891+
* with additional association conditions works.
892+
*
893+
* @return void
894+
*/
895+
public function testMergeBelongsToManyFromIdsWithConditions() {
896+
$this->articles->belongsToMany('Tags', [
897+
'conditions' => ['ArticleTags.article_id' => 1]
898+
]);
899+
900+
$entity = new Entity([
901+
'title' => 'No tags',
902+
'body' => 'Some content here',
903+
'tags' => []
904+
]);
905+
906+
$data = [
907+
'title' => 'Haz moar tags',
908+
'tags' => ['_ids' => [1, 2, 3]]
909+
];
910+
$entity->accessible('*', true);
911+
$marshall = new Marshaller($this->articles);
912+
$result = $marshall->merge($entity, $data, ['associated' => ['Tags']]);
913+
914+
$this->assertCount(3, $result->tags);
915+
$this->assertInstanceOf('Cake\ORM\Entity', $result->tags[0]);
916+
$this->assertInstanceOf('Cake\ORM\Entity', $result->tags[1]);
917+
$this->assertInstanceOf('Cake\ORM\Entity', $result->tags[2]);
918+
}
919+
889920
/**
890921
* Tests that merging data to an entity containing belongsToMany and _ids
891922
* will ignore empty values.

0 commit comments

Comments
 (0)