Skip to content

Commit

Permalink
Fixes #3714
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 15, 2014
1 parent 33584b2 commit 8071a0e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -420,6 +420,12 @@ public function saveAssociated(Entity $entity, array $options = []) {
* created if no errors happened, false otherwise
*/
protected function _saveTarget(Entity $parentEntity, $entities, $options) {
$associations = false;
if (!empty($options['associated'][$this->_junctionProperty]['associated'])) {
$associations = $options['associated'][$this->_junctionProperty]['associated'];
}
$options['associated'] = $associations;

if (!(is_array($entities) || $entities instanceof \Traversable)) {
$name = $this->property();
$message = sprintf('Could not save %s, it cannot be traversed', $name);
Expand Down Expand Up @@ -682,12 +688,6 @@ function() use ($sourceEntity, $targetEntities, $primaryValue, $options) {
$jointEntities = $this->_collectJointEntities($sourceEntity, $targetEntities);
$inserts = $this->_diffLinks($existing, $jointEntities, $targetEntities);

$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
25 changes: 19 additions & 6 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -177,20 +177,33 @@ public function testReciprocalBelongsToMany() {
}

/**
* Test for https://github.com/cakephp/cakephp/issues/3677
* Returns an array with the saving strategies for a belongsTo association
*
* @return array
*/
public function strategyProvider() {
return [['append', 'replace']];
}

/**
* Test for https://github.com/cakephp/cakephp/issues/3677 and
* https://github.com/cakephp/cakephp/issues/3714
*
* Checks that only relevant associations are passed when saving _joinData
* Tests that _joinData can also save deeper associations
*
* @dataProvider strategyProvider
* @param string $strategy
* @return void
*/
public function testBelongsToManyDeepSave() {
public function testBelongsToManyDeepSave($strategy) {
$articles = TableRegistry::get('Articles');
$articles->belongsToMany('Highlights', [
'className' => 'TestApp\Model\Table\TagsTable',
'foreignKey' => 'article_id',
'targetForeignKey' => 'tag_id',
'through' => 'SpecialTags'
'through' => 'SpecialTags',
'saveStrategy' => $strategy
]);
$articles->Highlights->junction()->belongsTo('Authors');
$articles->Highlights->hasOne('Authors', [
Expand All @@ -206,7 +219,7 @@ public function testBelongsToManyDeepSave() {
'highlighted' => true,
'highlighted_time' => '2014-06-01 10:10:00',
'author' => [
'name' => 'jose'
'name' => 'mariano'
]
],
'author' => ['name' => 'mark']
Expand All @@ -222,8 +235,8 @@ public function testBelongsToManyDeepSave() {
]
]);
$entity = $articles->get(2, ['contain' => ['SpecialTags.Authors', 'Highlights.Authors']]);
$this->assertEquals('jose', $entity->special_tags[0]->author->name);
$this->assertEquals('mark', $entity->highlights[0]->author->name);
$this->assertEquals('mariano', end($entity->special_tags)->author->name);
$this->assertEquals('mark', end($entity->highlights)->author->name);
}

}

0 comments on commit 8071a0e

Please sign in to comment.