Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix issue with postgres and virtualFields

If a virtualField was set to a literal value it would be quoted.
Test added.

Fixes #2085
  • Loading branch information...
commit fe9e59591319302760be9c470bb6f6c905b4d43d 1 parent 7e89442
@markstory markstory authored
View
7 lib/Cake/Model/Datasource/Database/Postgres.php
@@ -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
@@ -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]);
View
19 lib/Cake/Test/Case/Model/Datasource/Database/PostgresTest.php
@@ -752,6 +752,25 @@ public function testVirtualFields() {
}
/**
+ * 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
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.