diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index ae8df2cbe..444f0244a 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -1683,7 +1683,9 @@ class DboSource extends DataSource { if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) { for ($i = 0; $i < $count; $i++) { - if (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])){ + if (is_object($fields[$i]) && isset($fields[$i]->type) && $fields[$i]->type === 'expression') { + $fields[$i] = $fields[$i]->value; + } elseif (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])){ continue; } elseif (!preg_match('/^.+\\(.*\\)/', $fields[$i])) { $prepend = ''; diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index 00fe40866..3dbe92991 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -2781,6 +2781,20 @@ class DboSourceTest extends CakeTestCase { ); $this->assertEqual($result, $expected); } +/** + * test that fields() will accept objects made from DboSource::expression + * + * @return void + */ + function testFieldsWithExpression() { + $expression =& $this->testDb->expression("CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col"); + $result = $this->testDb->fields($this->Model, null, array("id", $expression)); + $expected = array( + '`TestModel`.`id`', + "CASE Sample.id WHEN 1 THEN 'Id One' ELSE 'Other Id' END AS case_col" + ); + $this->assertEqual($result, $expected); + } /** * testMergeAssociations method *