Skip to content

Commit

Permalink
Making DboSource::fields() accept an expression object. Fixes issues …
Browse files Browse the repository at this point in the history
…with sql parsing over quoting special SQL syntax.

Tests added
Fixes #66
  • Loading branch information
markstory committed Dec 13, 2009
1 parent 47a9401 commit 02330b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cake/libs/model/datasources/dbo_source.php
Expand Up @@ -1683,7 +1683,9 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {

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 = '';
Expand Down
14 changes: 14 additions & 0 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -2781,6 +2781,20 @@ function testFieldParsing() {
);
$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
*
Expand Down

0 comments on commit 02330b2

Please sign in to comment.