Skip to content
This repository
Browse code

make sure model data is moved to alias, even when first key in data i…

…s assoc model

add extra tests for saveAll and validate first
ensure db values dont get reset to default values
  • Loading branch information...
commit 7dbd6bc3a2bb17501fb411c6a0605e3908a9d0c5 1 parent 7107cd6
ceeram authored May 15, 2012 lorenzo committed May 19, 2012
38  lib/Cake/Model/Model.php
@@ -1129,9 +1129,7 @@ public function set($one, $two = null) {
1129 1129
 		if (is_array($one)) {
1130 1130
 			$data = $one;
1131 1131
 			if (empty($one[$this->alias])) {
1132  
-				if ($this->getAssociated(key($one)) === null) {
1133  
-					$data = array($this->alias => $one);
1134  
-				}
  1132
+				$data = $this->_setAliasData($one);
1135 1133
 			}
1136 1134
 		} else {
1137 1135
 			$data = array($this->alias => array($one => $two));
@@ -1161,6 +1159,24 @@ public function set($one, $two = null) {
1161 1159
 	}
1162 1160
 
1163 1161
 /**
  1162
+ * Move values to alias
  1163
+ *
  1164
+ * @param array $data
  1165
+ * @return array
  1166
+ */
  1167
+	protected function _setAliasData($data) {
  1168
+		$models = array_keys($this->getAssociated());
  1169
+		$schema = array_keys($this->schema());
  1170
+		foreach ($data as $field => $value) {
  1171
+			if (in_array($field, $schema) || !in_array($field, $models)) {
  1172
+				$data[$this->alias][$field] = $value;
  1173
+				unset($data[$field]);
  1174
+			}
  1175
+		}
  1176
+		return $data;
  1177
+	}
  1178
+
  1179
+/**
1164 1180
  * Normalize Xml::toArray() to use in Model::save()
1165 1181
  *
1166 1182
  * @param array $xml XML as array
@@ -2118,7 +2134,8 @@ public function validateMany(&$data, $options = array()) {
2118 2134
 			if ($options['deep']) {
2119 2135
 				$validates = $this->validateAssociated($record, $options);
2120 2136
 			} else {
2121  
-				$validates = $this->create($record) && $this->validates($options);
  2137
+				$this->create(null);
  2138
+				$validates = $this->set($record) && $this->validates($options);
2122 2139
 				$data[$key] = $this->data;
2123 2140
 			}
2124 2141
 			if ($validates === false || (is_array($validates) && in_array(false, $validates, true))) {
@@ -2318,21 +2335,14 @@ public function saveAssociated($data = null, $options = array()) {
2318 2335
 	public function validateAssociated(&$data, $options = array()) {
2319 2336
 		$options = array_merge(array('atomic' => true, 'deep' => false), $options);
2320 2337
 		$this->validationErrors = $validationErrors = $return = array();
2321  
-		if (!($this->create($data) && $this->validates($options))) {
  2338
+		$this->create(null);
  2339
+		if (!($this->set($data) && $this->validates($options))) {
2322 2340
 			$validationErrors[$this->alias] = $this->validationErrors;
2323 2341
 			$return[$this->alias] = false;
2324 2342
 		} else {
2325 2343
 			$return[$this->alias] = true;
2326 2344
 		}
2327  
-
2328  
-		if (empty($options['deep'])) {
2329  
-			$data = $this->data;
2330  
-		} else {
2331  
-			$modelData = $this->data;
2332  
-			$recordData = $modelData[$this->alias];
2333  
-			unset($modelData[$this->alias]);
2334  
-			$data = $modelData + array_merge($data, $recordData);
2335  
-		}
  2345
+		$data = $this->data;
2336 2346
 
2337 2347
 		$associations = $this->getAssociated();
2338 2348
 		foreach ($data as $association => &$values) {
2  lib/Cake/Test/Case/Model/ModelReadTest.php
@@ -7868,7 +7868,7 @@ public function testfindCustom() {
7868 7868
 		$Article = new CustomArticle();
7869 7869
 		$data = array('user_id' => 3, 'title' => 'Fourth Article', 'body' => 'Article Body, unpublished', 'published' => 'N');
7870 7870
 		$Article->create($data);
7871  
-		$Article->save();
  7871
+		$Article->save(null, false);
7872 7872
 		$this->assertEquals(4, $Article->id);
7873 7873
 
7874 7874
 		$result = $Article->find('published');
67  lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -1016,22 +1016,34 @@ public function testValidateFirstWithBeforeValidate() {
1016 1016
 		);
1017 1017
 		$result = $model->saveAll($data, array('validate' => 'first'));
1018 1018
 		$this->assertTrue($result);
  1019
+		$this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
1019 1020
 
1020  
-		$title = $model->field('title', array('body' => 'foo0'));
  1021
+		$model->findMethods['unPublished'] = true;
  1022
+		$data = array(
  1023
+			'CustomArticle' => array(
  1024
+				'body' => 'foo1'
  1025
+			)
  1026
+		);
  1027
+		$result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
  1028
+		$this->assertTrue($result);
  1029
+		$title = $model->field('title', array('body' => 'foo1'));
1021 1030
 		$this->assertEquals('foo', $title);
  1031
+		$this->assertFalse($model->findMethods['unPublished'], 'beforeValidate was run twice');
1022 1032
 
1023 1033
 		$data = array(
1024  
-			array('body' => 'foo1'),
1025 1034
 			array('body' => 'foo2'),
1026  
-			array('body' => 'foo3')
  1035
+			array('body' => 'foo3'),
  1036
+			array('body' => 'foo4')
1027 1037
 		);
1028 1038
 
1029 1039
 		$result = $model->saveAll($data, array('validate' => 'first'));
1030 1040
 		$this->assertTrue($result);
  1041
+		$result = $model->saveAll($data, array('validate' => 'first', 'deep' => true));
  1042
+		$this->assertTrue($result);
1031 1043
 
1032  
-		$this->assertEquals('foo', $model->field('title', array('body' => 'foo1')));
1033 1044
 		$this->assertEquals('foo', $model->field('title', array('body' => 'foo2')));
1034 1045
 		$this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
  1046
+		$this->assertEquals('foo', $model->field('title', array('body' => 'foo4')));
1035 1047
 	}
1036 1048
 
1037 1049
 /**
@@ -1040,7 +1052,7 @@ public function testValidateFirstWithBeforeValidate() {
1040 1052
  *
1041 1053
  * @return void
1042 1054
  */
1043  
-	public function testValidateAssociatedWithBeforeValidate() {
  1055
+	public function testValidateFirstAssociatedWithBeforeValidate() {
1044 1056
 		$this->loadFixtures('Article', 'User');
1045 1057
 		$model = new CustomArticle();
1046 1058
 		$model->validate = array(
@@ -1070,4 +1082,49 @@ public function testValidateAssociatedWithBeforeValidate() {
1070 1082
 		$this->assertEquals('foo', $model->field('title', array('body' => 'foo3')));
1071 1083
 	}
1072 1084
 
  1085
+/**
  1086
+ * testValidateFirstWithDefaults method
  1087
+ *
  1088
+ * return @void
  1089
+ */
  1090
+	public function testFirstWithDefaults() {
  1091
+		$this->loadFixtures('Article', 'Tag', 'Comment', 'User', 'ArticlesTag');
  1092
+		$TestModel = new Article();
  1093
+
  1094
+		$result = $TestModel->find('first', array(
  1095
+			'conditions' => array('Article.id' => 1)
  1096
+		));
  1097
+		$expected = array(
  1098
+			'Article' => array(
  1099
+				'id' => 1,
  1100
+				'user_id' => 1,
  1101
+				'title' => 'First Article',
  1102
+				'body' => 'First Article Body',
  1103
+				'published' => 'Y',
  1104
+				'created' => '2007-03-18 10:39:23'
  1105
+			),
  1106
+		);
  1107
+		unset($result['Article']['updated']);
  1108
+		$this->assertEquals($expected['Article'], $result['Article']);
  1109
+
  1110
+		$data = array(
  1111
+			'Article' => array(
  1112
+				'id' => 1,
  1113
+				'title' => 'First Article (modified)'
  1114
+			),
  1115
+			'Comment' => array(
  1116
+				array('comment' => 'Article comment', 'user_id' => 1)
  1117
+			)
  1118
+		);
  1119
+		$result = $TestModel->saveAll($data, array('validate' => 'first'));
  1120
+		$this->assertTrue($result);
  1121
+
  1122
+		$result = $TestModel->find('first', array(
  1123
+			'conditions' => array('Article.id' => 1)
  1124
+		));
  1125
+		$expected['Article']['title'] = 'First Article (modified)';
  1126
+		unset($result['Article']['updated']);
  1127
+		$this->assertEquals($expected['Article'], $result['Article']);
  1128
+	}
  1129
+
1073 1130
 }
10  lib/Cake/Test/Case/Model/ModelWriteTest.php
@@ -4277,7 +4277,7 @@ public function testSaveAllValidation() {
4277 4277
 					'author_id' => '3',
4278 4278
 					'title' => 'Just update the title',
4279 4279
 					'body' => 'Second Post Body',
4280  
-					'published' => 'N',
  4280
+					'published' => 'Y',
4281 4281
 					'created' => '2007-03-18 10:41:23'
4282 4282
 			)),
4283 4283
 			array(
@@ -4366,7 +4366,7 @@ public function testSaveAllValidation() {
4366 4366
 					'author_id' => '3',
4367 4367
 					'title' => 'Just update the title',
4368 4368
 					'body' => 'Second Post Body',
4369  
-					'published' => 'N',
  4369
+					'published' => 'Y',
4370 4370
 					'created' => '2007-03-18 10:41:23'
4371 4371
 				)
4372 4372
 			),
@@ -4734,7 +4734,7 @@ public function testSaveAssociated() {
4734 4734
 				'password' => '5f4dcc3b5aa765d61d8327deb882cf90'
4735 4735
 		)));
4736 4736
 
4737  
-		$result = $TestModel->find('all');
  4737
+		$result = $TestModel->find('all', array('order' => array('Post.id ' => 'ASC')));
4738 4738
 		$expected = array(
4739 4739
 			'Post' => array(
4740 4740
 				'id' => '4',
@@ -5631,7 +5631,7 @@ public function testSaveManyValidation() {
5631 5631
 					'author_id' => '3',
5632 5632
 					'title' => 'Just update the title',
5633 5633
 					'body' => 'Second Post Body',
5634  
-					'published' => 'N',
  5634
+					'published' => 'Y',
5635 5635
 					'created' => '2007-03-18 10:41:23'
5636 5636
 				)
5637 5637
 			),
@@ -5727,7 +5727,7 @@ public function testSaveManyValidation() {
5727 5727
 					'author_id' => '3',
5728 5728
 					'title' => 'Just update the title',
5729 5729
 					'body' => 'Second Post Body',
5730  
-					'published' => 'N',
  5730
+					'published' => 'Y',
5731 5731
 			)),
5732 5732
 			array(
5733 5733
 				'Post' => array(
7  lib/Cake/Test/Case/Model/models.php
@@ -3119,7 +3119,7 @@ class TranslatedItem2 extends CakeTestModel {
3119 3119
 /**
3120 3120
  * translateModel property
3121 3121
  *
3122  
- * @var string 
  3122
+ * @var string
3123 3123
  */
3124 3124
 	public $translateModel = 'TranslateWithPrefix';
3125 3125
 
@@ -4975,6 +4975,11 @@ protected function _findUnPublished($state, $query, $results = array()) {
4975 4975
  **/
4976 4976
 	public function beforeValidate($options = array()) {
4977 4977
 		$this->data[$this->alias]['title'] = 'foo';
  4978
+		if ($this->findMethods['unPublished'] === true) {
  4979
+			$this->findMethods['unPublished'] = false;
  4980
+		} else {
  4981
+			$this->findMethods['unPublished'] = 'true again';
  4982
+		}
4978 4983
 	}
4979 4984
 
4980 4985
 }

0 notes on commit 7dbd6bc

Please sign in to comment.
Something went wrong with that request. Please try again.