Browse Source

Merge pull request #3698 from cakephp/3.0-save-fixes

3.0 save fixes
Mark Story 11 years ago
parent
commit
ec64e1d642

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

@@ -681,7 +681,12 @@ class BelongsToMany extends Association {
 
 				$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;

+ 3 - 2
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)
 	);
 }
 

+ 45 - 0
tests/TestCase/ORM/QueryRegressionTest.php

@@ -176,4 +176,49 @@ class QueryRegressionTest extends TestCase {
 		$this->assertSame($left, $right);
 	}
 
+/**
+ * Test for https://github.com/cakephp/cakephp/issues/3677
+ *
+ * Checks that only relevant associations are passed when saving _joinData
+ * 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 = [
+			'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);
+	}
+
 }