Skip to content
This repository

deconstruct unknown date/time arrays to be able to properly use them in ... #842

Closed
wants to merge 2 commits into from

5 participants

Mark ADmad Mark Story Kyle Robinson Young José Lorenzo Rodríguez
lib/Cake/Model/Model.php
@@ -1210,14 +1210,26 @@ protected function _normalizeXmlData(array $xml) {
1210 1210
  *
1211 1211
  * @param string $field The name of the field to be deconstructed
1212 1212
  * @param array|object $data An array or object to be deconstructed into a field
  1213
+ * @param string $type The field type to manually overwrite the automatic detection or schema default
1
ADmad Collaborator
ADmad added a note September 14, 2012

s/overwrite/override

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Mark Story markstory commented on the diff September 14, 2012
lib/Cake/Model/Model.php
((9 lines not shown))
1216 1217
 		if (!is_array($data)) {
1217 1218
 			return $data;
1218 1219
 		}
1219 1220
 
1220  
-		$type = $this->getColumnType($field);
  1221
+		if ($type === null) {
  1222
+			$type = $this->getColumnType($field);
  1223
+		}
  1224
+		if ($type === null) {
  1225
+			// try to auto-detect
  1226
+			if (isset($data['day']) || isset($data['month']) || isset($data['year'])) {
  1227
+				$type = 'date';
  1228
+			}
  1229
+			if (isset($data['hour']) || isset($data['min']) || isset($data['sec'])) {
  1230
+				$type .= 'time';
  1231
+			}
  1232
+		}
1
Mark Story Owner

I dunno about this. Seems like moar magic which has the potential of going wrong. Like ADmad said in the lighthouse ticket couldn't you just setup the schema or specify the types when using deconstruct. In the past when we've had code that does guessing it generally ends up being wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Kyle Robinson Young shama commented on the diff September 14, 2012
lib/Cake/Test/Case/Model/ModelIntegrationTest.php
@@ -1167,6 +1167,68 @@ public function testDeconstructFieldsDateTime() {
1167 1167
 	}
1168 1168
 
1169 1169
 /**
  1170
+ * testDeconstructFields with unkown datetime fields
2
Kyle Robinson Young
shama added a note September 14, 2012

Small typo /unkown/unknown/ :)

José Lorenzo Rodríguez Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
ADmad
Collaborator

Since none of the devs seem to be in favor of this, I am closing the PR and the ticket.

ADmad ADmad closed this September 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
16  lib/Cake/Model/Model.php
@@ -1210,14 +1210,26 @@ protected function _normalizeXmlData(array $xml) {
1210 1210
  *
1211 1211
  * @param string $field The name of the field to be deconstructed
1212 1212
  * @param array|object $data An array or object to be deconstructed into a field
  1213
+ * @param string $type The field type to manually override the automatic detection or schema default
1213 1214
  * @return mixed The resulting data that should be assigned to a field
1214 1215
  */
1215  
-	public function deconstruct($field, $data) {
  1216
+	public function deconstruct($field, $data, $type = null) {
1216 1217
 		if (!is_array($data)) {
1217 1218
 			return $data;
1218 1219
 		}
1219 1220
 
1220  
-		$type = $this->getColumnType($field);
  1221
+		if ($type === null) {
  1222
+			$type = $this->getColumnType($field);
  1223
+		}
  1224
+		if ($type === null) {
  1225
+			// try to auto-detect
  1226
+			if (isset($data['day']) || isset($data['month']) || isset($data['year'])) {
  1227
+				$type = 'date';
  1228
+			}
  1229
+			if (isset($data['hour']) || isset($data['min']) || isset($data['sec'])) {
  1230
+				$type .= 'time';
  1231
+			}
  1232
+		}
1221 1233
 
1222 1234
 		if (in_array($type, array('datetime', 'timestamp', 'date', 'time'))) {
1223 1235
 			$useNewDate = (isset($data['year']) || isset($data['month']) ||
62  lib/Cake/Test/Case/Model/ModelIntegrationTest.php
@@ -1167,6 +1167,68 @@ public function testDeconstructFieldsDateTime() {
1167 1167
 	}
1168 1168
 
1169 1169
 /**
  1170
+ * testDeconstructFields with unkown datetime fields
  1171
+ *
  1172
+ * @return void
  1173
+ */
  1174
+	public function testDeconstructFieldsDateTimeWithUnkownFields() {
  1175
+		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  1176
+
  1177
+		$this->loadFixtures('Apple');
  1178
+		$TestModel = new Apple();
  1179
+
  1180
+		//test null/empty values first
  1181
+		$data['Apple']['published']['year'] = '';
  1182
+		$data['Apple']['published']['month'] = '';
  1183
+		$data['Apple']['published']['day'] = '';
  1184
+		$data['Apple']['published']['hour'] = '';
  1185
+		$data['Apple']['published']['min'] = '';
  1186
+		$data['Apple']['published']['sec'] = '';
  1187
+
  1188
+		$TestModel->data = null;
  1189
+		$TestModel->set($data);
  1190
+		$expected = array('Apple' => array('published' => ''));
  1191
+		$this->assertEquals($expected, $TestModel->data);
  1192
+
  1193
+		$data = array();
  1194
+		$data['Apple']['created']['year'] = '2007';
  1195
+		$data['Apple']['created']['month'] = '--';
  1196
+		$data['Apple']['created']['day'] = '20';
  1197
+		$data['Apple']['created']['hour'] = '10';
  1198
+		$data['Apple']['created']['min'] = '12';
  1199
+		$data['Apple']['created']['sec'] = '09';
  1200
+		$data['Apple']['published']['year'] = '2006';
  1201
+		$data['Apple']['published']['month'] = '12';
  1202
+		$data['Apple']['published']['day'] = '25';
  1203
+
  1204
+		$TestModel->data = null;
  1205
+		$TestModel->set($data);
  1206
+		$expected = array('Apple' => array('created' => '', 'published' => '2006-12-25'));
  1207
+		$this->assertEquals($expected, $TestModel->data);
  1208
+	}
  1209
+
  1210
+/**
  1211
+ * Test a manual deconstruct call
  1212
+ *
  1213
+ * @return void
  1214
+ */
  1215
+	public function testDeconstruct() {
  1216
+		$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  1217
+
  1218
+		$this->loadFixtures('Apple');
  1219
+		$TestModel = new Apple();
  1220
+
  1221
+		$data = array();
  1222
+		$data['Apple']['published']['year'] = '2006';
  1223
+		$data['Apple']['published']['month'] = '12';
  1224
+		$data['Apple']['published']['day'] = '25';
  1225
+		$data['Apple']['published'] = $TestModel->deconstruct('published', $data['Apple']['published'], 'datetime');
  1226
+
  1227
+		$expected = array('Apple' => array('published' => '2006-12-25 00:00:00'));
  1228
+		$this->assertEquals($expected, $data);
  1229
+	}
  1230
+
  1231
+/**
1170 1232
  * testTablePrefixSwitching method
1171 1233
  *
1172 1234
  * @return void
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.