Permalink
Browse files

Adding support for virtual fields in DboSourse::calculate

  • Loading branch information...
1 parent d06ff5d commit 7a7d0de98888e830ab441a76e95e03e30f709189 @lorenzo lorenzo committed Dec 7, 2009
Showing with 18 additions and 2 deletions.
  1. +12 −2 cake/libs/model/datasources/dbo_source.php
  2. +6 −0 cake/tests/cases/libs/model/model_read.test.php
@@ -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;
}
}
@@ -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");

0 comments on commit 7a7d0de

Please sign in to comment.