From 5a8a558fd9bfe7753f9fcf127ad905dd78a40e9f Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 5 Nov 2014 21:45:22 -0500 Subject: [PATCH] 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 --- src/ORM/Marshaller.php | 2 +- tests/TestCase/ORM/MarshallerTest.php | 31 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ORM/Marshaller.php b/src/ORM/Marshaller.php index 17d874a5292..af5b719d01d 100644 --- a/src/ORM/Marshaller.php +++ b/src/ORM/Marshaller.php @@ -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(); } /** diff --git a/tests/TestCase/ORM/MarshallerTest.php b/tests/TestCase/ORM/MarshallerTest.php index a4d87e92a20..b517b5a91f2 100644 --- a/tests/TestCase/ORM/MarshallerTest.php +++ b/tests/TestCase/ORM/MarshallerTest.php @@ -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.