Skip to content

Commit df8f6eb

Browse files
committed
Added fix an test for #3677
1 parent 6684e36 commit df8f6eb

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/ORM/Association/BelongsToMany.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,12 @@ function() use ($sourceEntity, $targetEntities, $primaryValue, $options) {
681681

682682
$jointEntities = $this->_collectJointEntities($sourceEntity, $targetEntities);
683683
$inserts = $this->_diffLinks($existing, $jointEntities, $targetEntities);
684-
$options += ['associated' => false];
684+
685+
$associations = false;
686+
if (!empty($options['associated'][$this->_junctionProperty]['associated'])) {
687+
$associations = $options['associated'][$this->_junctionProperty]['associated'];
688+
}
689+
$options['associated'] = $associations;
685690

686691
if ($inserts && !$this->_saveTarget($sourceEntity, $inserts, $options)) {
687692
return false;

tests/Fixture/SpecialTagFixture.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class SpecialTagFixture extends TestFixture {
3333
'tag_id' => ['type' => 'integer', 'null' => false],
3434
'highlighted' => ['type' => 'boolean', 'null' => true],
3535
'highlighted_time' => ['type' => 'timestamp', 'null' => true],
36+
'author_id' => ['type' => 'integer', 'null' => true],
3637
'_constraints' => [
3738
'primary' => ['type' => 'primary', 'columns' => ['id']],
3839
'UNIQUE_TAG2' => ['type' => 'unique', 'columns' => ['article_id', 'tag_id']]
@@ -45,8 +46,8 @@ class SpecialTagFixture extends TestFixture {
4546
* @var array
4647
*/
4748
public $records = array(
48-
array('article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null),
49-
array('article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00')
49+
array('article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null, 'author_id' => null),
50+
array('article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author_id' => null)
5051
);
5152
}
5253

tests/TestCase/ORM/QueryRegressionTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,49 @@ public function testReciprocalBelongsToMany() {
176176
$this->assertSame($left, $right);
177177
}
178178

179+
/**
180+
* Test for https://github.com/cakephp/cakephp/issues/3677
181+
*
182+
* Checks that only relevant associations are passed when saving _joinData
183+
* cand tests that _joinData can also save deeper associations
184+
* @return void
185+
*/
186+
public function testBelongsToManyDeepSave() {
187+
$articles = TableRegistry::get('Articles');
188+
$articles->belongsToMany('Highlights', [
189+
'className' => 'TestApp\Model\Table\TagsTable',
190+
'foreignKey' => 'article_id',
191+
'targetForeignKey' => 'tag_id',
192+
'through' => 'SpecialTags'
193+
]);
194+
$articles->Highlights->junction()->belongsTo('Authors');
195+
$entity = $articles->get(2, ['contain' => ['Highlights']]);
196+
197+
$data = [
198+
'id' => 2,
199+
'highlights' => [
200+
[
201+
'name' => 'New Special Tag',
202+
'_joinData' => [
203+
'highlighted' => true,
204+
'highlighted_time' => '2014-06-01 10:10:00',
205+
'author' => [
206+
'name' => 'jose'
207+
]
208+
]
209+
]
210+
]
211+
];
212+
$entity = $articles->patchEntity($entity, $data, [
213+
'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']]]]
214+
]);
215+
$articles->save($entity, [
216+
'associated' => [
217+
'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']]]]
218+
]
219+
]);
220+
$entity = $articles->get(2, ['contain' => ['SpecialTags.Authors']]);
221+
$this->assertEquals('jose', $entity->special_tags[0]->author->name);
222+
}
223+
179224
}

0 commit comments

Comments
 (0)