Skip to content

Commit

Permalink
Added fix an test for #3677
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 11, 2014
1 parent 6684e36 commit df8f6eb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/ORM/Association/BelongsToMany.php
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions tests/Fixture/SpecialTagFixture.php
Expand Up @@ -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']]
Expand All @@ -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)
);
}

45 changes: 45 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -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);
}

}

0 comments on commit df8f6eb

Please sign in to comment.