From 7a7d0de98888e830ab441a76e95e03e30f709189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Sun, 6 Dec 2009 20:55:49 -0430 Subject: [PATCH] Adding support for virtual fields in DboSourse::calculate --- cake/libs/model/datasources/dbo_source.php | 14 ++++++++++++-- cake/tests/cases/libs/model/model_read.test.php | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index ee7501bcee8..ddae55be969 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.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; } } diff --git a/cake/tests/cases/libs/model/model_read.test.php b/cake/tests/cases/libs/model/model_read.test.php index 1a6473ca4b8..e7b013c799b 100644 --- a/cake/tests/cases/libs/model/model_read.test.php +++ b/cake/tests/cases/libs/model/model_read.test.php @@ -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");