diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index 1de5a74862f..0e365792008 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -492,6 +492,7 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) { /** * Auxiliary function to quote matched `(Model.fields)` from a preg_replace_callback call + * Quotes the fields in a function call. * * @param string matched string * @return string quoted strig @@ -503,9 +504,11 @@ function __quoteFunctionField($match) { $prepend = 'DISTINCT '; $match[1] = trim(str_replace('DISTINCT', '', $match[1])); } - if (strpos($match[1], '.') === false) { + $constant = preg_match('/^\d+|NULL|FALSE|TRUE$/i', $match[1]); + + if (!$constant && strpos($match[1], '.') === false) { $match[1] = $this->name($match[1]); - } else { + } elseif (!$constant){ $parts = explode('.', $match[1]); if (!Set::numeric($parts)) { $match[1] = $this->name($match[1]); diff --git a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php index 2f3b9791d09..cef0d08e616 100644 --- a/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php @@ -814,6 +814,25 @@ function testVirtualFields() { $this->assertEqual($result['Article']['subquery'], 6); } +/** + * Test that virtual fields work with SQL constants + * + * @return void + */ + function testVirtualFieldAsAConstant() { + $this->loadFixtures('Article', 'Comment'); + $Article =& ClassRegistry::init('Article'); + $Article->virtualFields = array( + 'empty' => "NULL", + 'number' => 43, + 'truth' => 'TRUE' + ); + $result = $Article->find('first'); + $this->assertNull($result['Article']['empty']); + $this->assertTrue($result['Article']['truth']); + $this->assertEqual(42, $result['Article']['number']); + } + /** * Tests additional order options for postgres *