Skip to content

Commit d2b4995

Browse files
committed
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
1 parent a9bbae3 commit d2b4995

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

cake/libs/model/model.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,13 +815,18 @@ function set($one, $two = null) {
815815
* @access public
816816
*/
817817
function deconstruct($field, $data) {
818+
if (!is_array($data)) {
819+
return $data;
820+
}
821+
818822
$copy = $data;
819823
$type = $this->getColumnType($field);
820-
$db =& ConnectionManager::getDataSource($this->useDbConfig);
821824

822825
if (in_array($type, array('datetime', 'timestamp', 'date', 'time'))) {
823826
$useNewDate = (isset($data['year']) || isset($data['month']) || isset($data['day']) || isset($data['hour']) || isset($data['minute']));
824827
$dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec');
828+
829+
$db =& ConnectionManager::getDataSource($this->useDbConfig);
825830
$format = $db->columns[$type]['format'];
826831
$date = array();
827832

@@ -1380,7 +1385,7 @@ function updateCounterCache($keys = array(), $created = false) {
13801385
if ($keys['old'][$foreignKey] != $keys[$foreignKey]) {
13811386
$conditions[$fkQuoted] = $keys['old'][$foreignKey];
13821387
$count = intval($this->find('count', compact('conditions', 'recursive')));
1383-
1388+
13841389
$this->{$parent}->updateAll(
13851390
array($assoc['counterCache'] => $count),
13861391
array($this->{$parent}->escapeField() => $keys['old'][$foreignKey])

cake/tests/cases/libs/model/model.test.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function endTest() {
102102

103103
/**
104104
* Tests getAssociated method
105-
*
105+
*
106106
* @access public
107107
* @return void
108108
*/
@@ -4051,10 +4051,10 @@ function testSaveWithCounterCacheScope() {
40514051
$TestModel2->saveField('published', true);
40524052
$result = $TestModel->findById(1);
40534053
$this->assertIdentical($result['Syfile']['item_count'], '2');
4054-
4054+
40554055
$TestModel2->save(array('id' => 1, 'syfile_id' => 1, 'published'=> false));
40564056
$result = $TestModel->findById(1);
4057-
$this->assertIdentical($result['Syfile']['item_count'], '1');
4057+
$this->assertIdentical($result['Syfile']['item_count'], '1');
40584058
}
40594059
/**
40604060
* testDel method
@@ -5906,6 +5906,19 @@ function testDeconstructFields() {
59065906
$TestModel->set($data);
59075907
$expected = array('Apple'=> array('mytime'=> '03:04:04'));
59085908
$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);
59095922
}
59105923
/**
59115924
* testTablePrefixSwitching method

0 commit comments

Comments
 (0)