Permalink
Browse files

Added virtual field support for GROUP BY.

  • Loading branch information...
Phally authored and lorenzo committed Jan 9, 2010
1 parent dda2414 commit bbb105fc8c1bf125172ff4306ec92355fe6dc620
Showing with 30 additions and 2 deletions.
  1. +7 −2 cake/libs/model/datasources/dbo_source.php
  2. +23 −0 cake/tests/cases/libs/model/model_read.test.php
View
@@ -1413,7 +1413,7 @@ function buildStatement($query, &$model) {
'order' => $this->order($query['order'], 'ASC', $model),
'limit' => $this->limit($query['limit'], $query['offset']),
'joins' => implode(' ', $query['joins']),
- 'group' => $this->group($query['group'])
+ 'group' => $this->group($query['group'], $model)
));
}
@@ -2328,9 +2328,14 @@ function order($keys, $direction = 'ASC', $model = null) {
* @return mixed string condition or null
* @access public
*/
- function group($group) {
+ function group($group, &$model = null) {
if ($group) {
if (is_array($group)) {
+ foreach($group as $index => $key) {
+ if ($model->isVirtualField($key)) {
+ $group[$index] = '(' . $model->getVirtualField($key) . ')';
+ }
+ }
$group = implode(', ', $group);
}
return ' GROUP BY ' . $this->__quoteFields($group);
View
@@ -7240,6 +7240,29 @@ function testVirtualFields() {
$Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
$result = $Post->field('other_field');
$this->assertEqual($result, 4);
+
+ ClassRegistry::flush();
+ $Post = ClassRegistry::init('Post');
+
+ $Post->create();
+ $Post->virtualFields = array(
+ 'year' => 'YEAR(Post.created)',
+ 'unique_test_field' => 'COUNT(Post.id)'
+ );
+
+ $expectation = array(
+ 'Post' => array(
+ 'year' => 2007,
+ 'unique_test_field' => 3
+ )
+ );
+
+ $result = $Post->find('first', array(
+ 'fields' => array_keys($Post->virtualFields),
+ 'group' => array('year')
+ ));
+
+ $this->assertEqual($result, $expectation);
}
}
?>

0 comments on commit bbb105f

Please sign in to comment.