Browse Source

Stop fatal errors when _joinData is present and not an entity.

If _joinData is set to an array property it should not cause failures
when attempting to save. Array values should be converted into entities
elsewhere in the process.

Refs #5130
Mark Story 11 years ago
parent
commit
d7594cc20a
2 changed files with 22 additions and 5 deletions
  1. 3 5
      src/ORM/Association/BelongsToMany.php
  2. 19 0
      tests/TestCase/ORM/TableTest.php

+ 3 - 5
src/ORM/Association/BelongsToMany.php

@@ -496,10 +496,8 @@ class BelongsToMany extends Association {
 
 		foreach ($targetEntities as $e) {
 			$joint = $e->get($jointProperty);
-			if (!$joint) {
-				$joint = new $entityClass;
-				$joint->isNew(true);
-				$joint->source($junctionAlias);
+			if (!$joint || !($joint instanceof Entity)) {
+				$joint = new $entityClass([], ['markNew' => true, 'source' => $junctionAlias]);
 			}
 
 			$joint->set(array_combine(
@@ -828,7 +826,7 @@ class BelongsToMany extends Association {
 		foreach ($targetEntities as $entity) {
 			$joint = $entity->get($jointProperty);
 
-			if (!$joint) {
+			if (!$joint || !($joint instanceof Entity)) {
 				$missing[] = $entity->extract($primary);
 				continue;
 			}

+ 19 - 0
tests/TestCase/ORM/TableTest.php

@@ -2801,6 +2801,25 @@ class TableTest extends TestCase {
 	}
 
 /**
+ * Test that belongsToMany can be saved with _joinData data.
+ *
+ * @return void
+ */
+	public function testSaveBelongsToManyJoinData() {
+		$articles = TableRegistry::get('Articles');
+		$article = $articles->get(1, ['contain' => ['Tags']]);
+		$data = [
+			'tags' => [
+				['id' => 1, '_joinData' => ['highlighted' => 1]],
+				['id' => 3]
+			]
+		];
+		$article = $articles->patchEntity($article, $data);
+		$result = $articles->save($article);
+		$this->assertSame($result, $article);
+	}
+
+/**
  * Tests saving belongsToMany records can delete all links.
  *
  * @group save