Skip to content

Commit c66fe39

Browse files
committed
Fix issue with postgres and virtualFields
If a virtualField was set to a literal value it would be quoted. Test added. Fixes #2085
1 parent 118cbe2 commit c66fe39

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

cake/libs/model/datasources/dbo/dbo_postgres.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
492492

493493
/**
494494
* Auxiliary function to quote matched `(Model.fields)` from a preg_replace_callback call
495+
* Quotes the fields in a function call.
495496
*
496497
* @param string matched string
497498
* @return string quoted strig
@@ -503,9 +504,11 @@ function __quoteFunctionField($match) {
503504
$prepend = 'DISTINCT ';
504505
$match[1] = trim(str_replace('DISTINCT', '', $match[1]));
505506
}
506-
if (strpos($match[1], '.') === false) {
507+
$constant = preg_match('/^\d+|NULL|FALSE|TRUE$/i', $match[1]);
508+
509+
if (!$constant && strpos($match[1], '.') === false) {
507510
$match[1] = $this->name($match[1]);
508-
} else {
511+
} elseif (!$constant){
509512
$parts = explode('.', $match[1]);
510513
if (!Set::numeric($parts)) {
511514
$match[1] = $this->name($match[1]);

cake/tests/cases/libs/model/datasources/dbo/dbo_postgres.test.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,25 @@ function testVirtualFields() {
814814
$this->assertEqual($result['Article']['subquery'], 6);
815815
}
816816

817+
/**
818+
* Test that virtual fields work with SQL constants
819+
*
820+
* @return void
821+
*/
822+
function testVirtualFieldAsAConstant() {
823+
$this->loadFixtures('Article', 'Comment');
824+
$Article =& ClassRegistry::init('Article');
825+
$Article->virtualFields = array(
826+
'empty' => "NULL",
827+
'number' => 43,
828+
'truth' => 'TRUE'
829+
);
830+
$result = $Article->find('first');
831+
$this->assertNull($result['Article']['empty']);
832+
$this->assertTrue($result['Article']['truth']);
833+
$this->assertEqual(42, $result['Article']['number']);
834+
}
835+
817836
/**
818837
* Tests additional order options for postgres
819838
*

0 commit comments

Comments
 (0)