From df8f6eb5b4d533cddb7f90ea5efdc80cacbbc16c Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 12 Jun 2014 00:16:20 +0200 Subject: [PATCH] Added fix an test for #3677 --- src/ORM/Association/BelongsToMany.php | 7 +++- tests/Fixture/SpecialTagFixture.php | 5 ++- tests/TestCase/ORM/QueryRegressionTest.php | 45 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/ORM/Association/BelongsToMany.php b/src/ORM/Association/BelongsToMany.php index e53b69e41e1..935bc63767b 100644 --- a/src/ORM/Association/BelongsToMany.php +++ b/src/ORM/Association/BelongsToMany.php @@ -681,7 +681,12 @@ function() use ($sourceEntity, $targetEntities, $primaryValue, $options) { $jointEntities = $this->_collectJointEntities($sourceEntity, $targetEntities); $inserts = $this->_diffLinks($existing, $jointEntities, $targetEntities); - $options += ['associated' => false]; + + $associations = false; + if (!empty($options['associated'][$this->_junctionProperty]['associated'])) { + $associations = $options['associated'][$this->_junctionProperty]['associated']; + } + $options['associated'] = $associations; if ($inserts && !$this->_saveTarget($sourceEntity, $inserts, $options)) { return false; diff --git a/tests/Fixture/SpecialTagFixture.php b/tests/Fixture/SpecialTagFixture.php index 265a2a53b94..854f0e43288 100644 --- a/tests/Fixture/SpecialTagFixture.php +++ b/tests/Fixture/SpecialTagFixture.php @@ -33,6 +33,7 @@ class SpecialTagFixture extends TestFixture { 'tag_id' => ['type' => 'integer', 'null' => false], 'highlighted' => ['type' => 'boolean', 'null' => true], 'highlighted_time' => ['type' => 'timestamp', 'null' => true], + 'author_id' => ['type' => 'integer', 'null' => true], '_constraints' => [ 'primary' => ['type' => 'primary', 'columns' => ['id']], 'UNIQUE_TAG2' => ['type' => 'unique', 'columns' => ['article_id', 'tag_id']] @@ -45,8 +46,8 @@ class SpecialTagFixture extends TestFixture { * @var array */ public $records = array( - array('article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null), - array('article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00') + array('article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null, 'author_id' => null), + array('article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author_id' => null) ); } diff --git a/tests/TestCase/ORM/QueryRegressionTest.php b/tests/TestCase/ORM/QueryRegressionTest.php index 6d24fc458db..68f5df9a502 100644 --- a/tests/TestCase/ORM/QueryRegressionTest.php +++ b/tests/TestCase/ORM/QueryRegressionTest.php @@ -176,4 +176,49 @@ public function testReciprocalBelongsToMany() { $this->assertSame($left, $right); } +/** + * Test for https://github.com/cakephp/cakephp/issues/3677 + * + * Checks that only relevant associations are passed when saving _joinData + * cand tests that _joinData can also save deeper associations + * @return void + */ + public function testBelongsToManyDeepSave() { + $articles = TableRegistry::get('Articles'); + $articles->belongsToMany('Highlights', [ + 'className' => 'TestApp\Model\Table\TagsTable', + 'foreignKey' => 'article_id', + 'targetForeignKey' => 'tag_id', + 'through' => 'SpecialTags' + ]); + $articles->Highlights->junction()->belongsTo('Authors'); + $entity = $articles->get(2, ['contain' => ['Highlights']]); + + $data = [ + 'id' => 2, + 'highlights' => [ + [ + 'name' => 'New Special Tag', + '_joinData' => [ + 'highlighted' => true, + 'highlighted_time' => '2014-06-01 10:10:00', + 'author' => [ + 'name' => 'jose' + ] + ] + ] + ] + ]; + $entity = $articles->patchEntity($entity, $data, [ + 'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']]]] + ]); + $articles->save($entity, [ + 'associated' => [ + 'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']]]] + ] + ]); + $entity = $articles->get(2, ['contain' => ['SpecialTags.Authors']]); + $this->assertEquals('jose', $entity->special_tags[0]->author->name); + } + }