Skip to content

Commit

Permalink
Adding support for virtual fields in DboSourse::calculate
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 11, 2009
1 parent d06ff5d commit 7a7d0de
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -1667,13 +1667,23 @@ function calculate(&$model, $func, $params = array()) {
if (!isset($params[1])) {
$params[1] = 'count';
}
return 'COUNT(' . $this->name($params[0]) . ') AS ' . $this->name($params[1]);
if (!empty($model->virtualFields[$params[0]])) {
$arg = $model->virtualFields[$params[0]];
} else {
$arg = $this->name($params[0]);
}
return 'COUNT(' . $arg . ') AS ' . $this->name($params[1]);
case 'max':
case 'min':
if (!isset($params[1])) {
$params[1] = $params[0];
}
return strtoupper($func) . '(' . $this->name($params[0]) . ') AS ' . $this->name($params[1]);
if (!empty($model->virtualFields[$params[0]])) {
$arg = $model->virtualFields[$params[0]];
} else {
$arg = $this->name($params[0]);
}
return strtoupper($func) . '(' . $arg . ') AS ' . $this->name($params[1]);
break;
}
}
Expand Down
6 changes: 6 additions & 0 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -88,6 +88,12 @@ function testVirtualFields() {
));
$this->assertEqual($result['Post']['id'],2);

$Post->virtualFields = array('other_field' => $dbo->name('Post.id') . ' + 1');
$result = $Post->find('all',array(
'fields' => array($dbo->calculate($Post,'max',array('other_field')))
));
$this->assertEqual($result[0]['other_field'],4);

ClassRegistry::flush();
$Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model');
$Writing->virtualFields = array('two' => "1 + 1");
Expand Down

0 comments on commit 7a7d0de

Please sign in to comment.