Skip to content

Commit

Permalink
Fixing virtualFields used in order clauses where virtualField was sup…
Browse files Browse the repository at this point in the history
…plied with model alias. Tests added. Refs #768
  • Loading branch information
markstory committed May 31, 2010
1 parent c98a82c commit 7ed67e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
16 changes: 8 additions & 8 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -2421,17 +2421,17 @@ function order($keys, $direction = 'ASC', $model = null) {
$key = preg_replace('/\\x20(ASC|DESC).*/i', '', $key);
}

$key = trim($key);

if (is_object($model) && $model->isVirtualField($key)) {
$key = '(' . $this->__quoteFields($model->getVirtualField($key)) . ')';
}

if (strpos($key, '.')) {
$key = preg_replace_callback('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', array(&$this, '__quoteMatchedField'), $key);
}

$key = trim($key);
if (!preg_match('/\s/', $key) && !strpos($key,'.')) {
if (is_object($model) && $model->isVirtualField($key)) {
$key = '('.$this->__quoteFields($model->getVirtualField($key)).')';
} else {
$key = $this->name($key);
}
if (!preg_match('/\s/', $key) && !strpos($key, '.')) {
$key = $this->name($key);
}
$key .= ' ' . trim($dir);
$result[] = $key;
Expand Down
5 changes: 5 additions & 0 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -4353,6 +4353,11 @@ function testVirtualFieldsInOrder() {
$result = $this->db->order($order, 'ASC', $Article);
$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
$this->assertEqual($expected, $result);

$order = array('Article.two', 'Article.this_moment');
$result = $this->db->order($order, 'ASC', $Article);
$expected = ' ORDER BY (1 + 1) ASC, (NOW()) ASC';
$this->assertEqual($expected, $result);
}

/**
Expand Down

0 comments on commit 7ed67e5

Please sign in to comment.