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
This commit is contained in:
jperras 2009-06-02 03:23:15 +00:00
parent a9bbae31ee
commit d2b4995e43
2 changed files with 23 additions and 5 deletions

View file

@ -815,13 +815,18 @@ class Model extends Overloadable {
* @access public
*/
function deconstruct($field, $data) {
if (!is_array($data)) {
return $data;
}
$copy = $data;
$type = $this->getColumnType($field);
$db =& ConnectionManager::getDataSource($this->useDbConfig);
if (in_array($type, array('datetime', 'timestamp', 'date', 'time'))) {
$useNewDate = (isset($data['year']) || isset($data['month']) || isset($data['day']) || isset($data['hour']) || isset($data['minute']));
$dateFields = array('Y' => 'year', 'm' => 'month', 'd' => 'day', 'H' => 'hour', 'i' => 'min', 's' => 'sec');
$db =& ConnectionManager::getDataSource($this->useDbConfig);
$format = $db->columns[$type]['format'];
$date = array();
@ -1380,7 +1385,7 @@ class Model extends Overloadable {
if ($keys['old'][$foreignKey] != $keys[$foreignKey]) {
$conditions[$fkQuoted] = $keys['old'][$foreignKey];
$count = intval($this->find('count', compact('conditions', 'recursive')));
$this->{$parent}->updateAll(
array($assoc['counterCache'] => $count),
array($this->{$parent}->escapeField() => $keys['old'][$foreignKey])

View file

@ -102,7 +102,7 @@ class ModelTest extends CakeTestCase {
/**
* Tests getAssociated method
*
*
* @access public
* @return void
*/
@ -4051,10 +4051,10 @@ class ModelTest extends CakeTestCase {
$TestModel2->saveField('published', true);
$result = $TestModel->findById(1);
$this->assertIdentical($result['Syfile']['item_count'], '2');
$TestModel2->save(array('id' => 1, 'syfile_id' => 1, 'published'=> false));
$result = $TestModel->findById(1);
$this->assertIdentical($result['Syfile']['item_count'], '1');
$this->assertIdentical($result['Syfile']['item_count'], '1');
}
/**
* testDel method
@ -5906,6 +5906,19 @@ class ModelTest extends CakeTestCase {
$TestModel->set($data);
$expected = array('Apple'=> array('mytime'=> '03:04:04'));
$this->assertEqual($TestModel->data, $expected);
$db = ConnectionManager::getDataSource('test_suite');
$data = array();
$data['Apple']['modified'] = $db->expression('NOW()');
$TestModel->data = null;
$TestModel->set($data);
$this->assertEqual($TestModel->data, $data);
$data = array();
$data['Apple']['mytime'] = $db->expression('NOW()');
$TestModel->data = null;
$TestModel->set($data);
$this->assertEqual($TestModel->data, $data);
}
/**
* testTablePrefixSwitching method