Browse Source

Starting to implement dot notation for association options in Table::save()

Jose Lorenzo Rodriguez 11 years ago
parent
commit
a751d3738e

+ 30 - 5
src/ORM/Associations.php

@@ -263,12 +263,37 @@ class Associations {
 		}
 
 		$result = [];
-		foreach ($keys as $key => $value) {
-			if (is_int($key)) {
-				$key = $value;
-				$value = [];
+		foreach ($keys as $table => $options) {
+			$pointer =& $result;
+
+			if (is_int($table)) {
+				$table = $options;
+				$options = [];
+			}
+
+			if (!strpos($table, '.')) {
+				$result[$table] = $options;
+				continue;
+			}
+
+			$path = explode('.', $table);
+			$table = array_pop($path);
+			$first = array_shift($path);
+
+			if (isset($pointer[$first])) {
+				$pointer =& $pointer[$first];
+				$pointer += ['associated' => []];
 			}
-			$result[$key] = $value;
+
+			foreach ($path as $t) {
+				$pointer += ['associated' => []];
+				$pointer['associated'] += [$t => []];
+				$pointer['associated'][$t] += ['associated' => []];
+				$pointer =& $pointer['associated'][$t];
+			}
+
+			$pointer['associated'] += [$table => []];
+			$pointer['associated'][$table] = $options + $pointer['associated'][$table];
 		}
 
 		return $result;

+ 1 - 1
tests/TestCase/ORM/QueryRegressionTest.php

@@ -218,7 +218,7 @@ class QueryRegressionTest extends TestCase {
 		]);
 		$articles->save($entity, [
 			'associated' => [
-				'Highlights' => ['associated' => ['_joinData' => ['associated' => ['Authors']], 'Authors']]
+				'Highlights._joinData.Authors', 'Highlights.Authors'
 			]
 		]);
 		$entity = $articles->get(2, ['contain' => ['SpecialTags.Authors', 'Highlights.Authors']]);

+ 6 - 8
tests/TestCase/ORM/TableTest.php

@@ -2919,14 +2919,12 @@ class TableTest extends \Cake\TestSuite\TestCase {
 		$this->assertSame($entity, $articles->save($entity, [
 			'associated' => [
 				'authors' => [
-					'validate' => 'special',
-					'associated' => [
-						'supervisors' => [
-							'atomic' => false,
-							'validate' => false,
-							'associated' => false
-						]
-					]
+					'validate' => 'special'
+				],
+				'authors.supervisors' => [
+					'atomic' => false,
+					'validate' => false,
+					'associated' => false
 				]
 			]
 		]));