Jose Lorenzo Rodriguez 11 years ago
parent
commit
8071a0e8f5
2 changed files with 25 additions and 12 deletions
  1. 6 6
      src/ORM/Association/BelongsToMany.php
  2. 19 6
      tests/TestCase/ORM/QueryRegressionTest.php

+ 6 - 6
src/ORM/Association/BelongsToMany.php

@@ -420,6 +420,12 @@ class BelongsToMany extends Association {
  * 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);
@@ -682,12 +688,6 @@ class BelongsToMany extends Association {
 				$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;
 				}

+ 19 - 6
tests/TestCase/ORM/QueryRegressionTest.php

@@ -177,20 +177,33 @@ class QueryRegressionTest extends TestCase {
 	}
 
 /**
- * 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', [
@@ -206,7 +219,7 @@ class QueryRegressionTest extends TestCase {
 						'highlighted' => true,
 						'highlighted_time' => '2014-06-01 10:10:00',
 						'author' => [
-							'name' => 'jose'
+							'name' => 'mariano'
 						]
 					],
 					'author' => ['name' => 'mark']
@@ -222,8 +235,8 @@ class QueryRegressionTest extends TestCase {
 			]
 		]);
 		$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);
 	}
 
 }