Skip to content
This repository
Browse code

Updating Model::deconstruct to handle db expressions correctly. Fixes…

… #6372; thanks to mattcurry for the patch.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8185 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
commit d2b4995e439430caab2613b13fe006ba36cf25c9 1 parent a9bbae3
Joel Perras authored June 02, 2009
9  cake/libs/model/model.php
@@ -815,13 +815,18 @@ function set($one, $two = null) {
815 815
  * @access public
816 816
  */
817 817
 	function deconstruct($field, $data) {
  818
+		if (!is_array($data)) {
  819
+			return $data;
  820
+		}
  821
+
818 822
 		$copy = $data;
819 823
 		$type = $this->getColumnType($field);
820  
-		$db =& ConnectionManager::getDataSource($this->useDbConfig);
821 824
 
822 825
 		if (in_array($type, array('datetime', 'timestamp', 'date', 'time'))) {
823 826
 			$useNewDate = (isset($data['year']) || isset($data['month']) || isset($data['day']) || isset($data['hour']) || isset($data['minute']));
824 827
 			$dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec');
  828
+
  829
+			$db =& ConnectionManager::getDataSource($this->useDbConfig);
825 830
 			$format = $db->columns[$type]['format'];
826 831
 			$date = array();
827 832
 
@@ -1380,7 +1385,7 @@ function updateCounterCache($keys = array(), $created = false) {
1380 1385
 					if ($keys['old'][$foreignKey] != $keys[$foreignKey]) {
1381 1386
 						$conditions[$fkQuoted] = $keys['old'][$foreignKey];
1382 1387
 						$count = intval($this->find('count', compact('conditions', 'recursive')));
1383  
-	
  1388
+
1384 1389
 						$this->{$parent}->updateAll(
1385 1390
 							array($assoc['counterCache'] => $count),
1386 1391
 							array($this->{$parent}->escapeField() => $keys['old'][$foreignKey])
19  cake/tests/cases/libs/model/model.test.php
@@ -102,7 +102,7 @@ function endTest() {
102 102
 
103 103
 /**
104 104
  * Tests getAssociated method
105  
- * 
  105
+ *
106 106
  * @access public
107 107
  * @return void
108 108
  */
@@ -4051,10 +4051,10 @@ function testSaveWithCounterCacheScope() {
4051 4051
 		$TestModel2->saveField('published', true);
4052 4052
 		$result = $TestModel->findById(1);
4053 4053
 		$this->assertIdentical($result['Syfile']['item_count'], '2');
4054  
-    
  4054
+
4055 4055
 		$TestModel2->save(array('id' => 1, 'syfile_id' => 1, 'published'=> false));
4056 4056
 		$result = $TestModel->findById(1);
4057  
-		$this->assertIdentical($result['Syfile']['item_count'], '1');    
  4057
+		$this->assertIdentical($result['Syfile']['item_count'], '1');
4058 4058
 	}
4059 4059
 /**
4060 4060
  * testDel method
@@ -5906,6 +5906,19 @@ function testDeconstructFields() {
5906 5906
 		$TestModel->set($data);
5907 5907
 		$expected = array('Apple'=> array('mytime'=> '03:04:04'));
5908 5908
 		$this->assertEqual($TestModel->data, $expected);
  5909
+
  5910
+		$db = ConnectionManager::getDataSource('test_suite');
  5911
+		$data = array();
  5912
+		$data['Apple']['modified'] = $db->expression('NOW()');
  5913
+		$TestModel->data = null;
  5914
+		$TestModel->set($data);
  5915
+		$this->assertEqual($TestModel->data, $data);
  5916
+
  5917
+		$data = array();
  5918
+		$data['Apple']['mytime'] = $db->expression('NOW()');
  5919
+		$TestModel->data = null;
  5920
+		$TestModel->set($data);
  5921
+		$this->assertEqual($TestModel->data, $data);
5909 5922
 	}
5910 5923
 /**
5911 5924
  * testTablePrefixSwitching method

0 notes on commit d2b4995

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