Skip to content

Commit

Permalink
Fix issue with postgres and virtualFields
Browse files Browse the repository at this point in the history
If a virtualField was set to a literal value it would be quoted.
Test added.

Fixes #2085
  • Loading branch information
markstory committed Oct 22, 2011
1 parent 7e89442 commit fe9e595
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/Cake/Model/Datasource/Database/Postgres.php
Expand Up @@ -394,6 +394,7 @@ public 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 $match matched string
* @return string quoted strig
Expand All @@ -404,9 +405,11 @@ protected 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]);
Expand Down
19 changes: 19 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php
Expand Up @@ -751,6 +751,25 @@ public 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->assertEquals(43, $result['Article']['number']);
}

/**
* Tests additional order options for postgres
*
Expand Down

0 comments on commit fe9e595

Please sign in to comment.