Permalink
Browse files

Quoting virtual fields in automatically

Updating test cases
  • Loading branch information...
1 parent 8c4cad8 commit c65e2f3f182b268202c06ca11cd3b4ecd9d83ed7 @lorenzo lorenzo committed Dec 11, 2009
Showing with 43 additions and 7 deletions.
  1. +5 −5 cake/libs/model/datasources/dbo_source.php
  2. +38 −2 cake/tests/cases/libs/model/model_read.test.php
View
10 cake/libs/model/datasources/dbo_source.php
@@ -1639,7 +1639,7 @@ function calculate(&$model, $func, $params = array()) {
$params[1] = 'count';
}
if (is_object($model) && $model->isVirtualField($params[0])){
- $arg = $model->getVirtualField($params[0]);
+ $arg = $this->__quoteFields($model->getVirtualField($params[0]));
} else {
$arg = $this->name($params[0]);
}
@@ -1650,7 +1650,7 @@ function calculate(&$model, $func, $params = array()) {
$params[1] = $params[0];
}
if (is_object($model) && $model->isVirtualField($params[0])) {
- $arg = $model->getVirtualField($params[0]);
+ $arg = $this->__quoteFields($model->getVirtualField($params[0]));
} else {
$arg = $this->name($params[0]);
}
@@ -1787,7 +1787,7 @@ function _constructVirtualFields(&$model,$alias,$fields) {
$virtual = array();
foreach ($fields as $field) {
$virtualField = $this->name("{$alias}__{$field}");
- $expression = $model->getVirtualField($field);
+ $expression = $this->__quoteFields($model->getVirtualField($field));
$virtual[] = $expression . " {$this->alias} {$virtualField}";
}
return $virtual;
@@ -2068,7 +2068,7 @@ function __parseKey($model, $key, $value) {
$virtual = false;
if (is_object($model) && $model->isVirtualField($key)) {
- $key = $model->getVirtualField($key);
+ $key = $this->__quoteFields($model->getVirtualField($key));
$virtual = true;
}
@@ -2241,7 +2241,7 @@ function order($keys, $direction = 'ASC', &$model = null) {
$key = trim($key);
if (!preg_match('/\s/', $key) && !strpos($key,'.')) {
if (is_object($model) && $model->isVirtualField($key)) {
- $key = $model->getVirtualField($key);
+ $key = $this->__quoteFields($model->getVirtualField($key));
} else {
$key = $this->name($key);
}
View
40 cake/tests/cases/libs/model/model_read.test.php
@@ -26,7 +26,6 @@
*/
class ModelReadTest extends BaseModelTest {
-
/**
* testFetchingNonUniqueFKJoinTableRecords()
*
@@ -7172,7 +7171,7 @@ function testFindQueryTypeInCallbacks() {
* @access public
* @return void
*/
- function testVirtualFields() {
+ function testVirtualFields() {
$this->loadFixtures('Post','Author');
$Post = ClassRegistry::init('Post');
$Post->virtualFields = array('two' => "1 + 1");
@@ -7199,11 +7198,48 @@ function testVirtualFields() {
$result = $Post->field('two');
$this->assertEqual($result,2);
+ $result = $Post->find('first',array(
+ 'conditions' => array('two' => 2),
+ 'limit' => 1
+ ));
+ $this->assertEqual($result['Post']['two'],2);
+
+ $result = $Post->find('first',array(
+ 'conditions' => array('two <' => 3),
+ 'limit' => 1
+ ));
+ $this->assertEqual($result['Post']['two'],2);
+
+ $result = $Post->find('first',array(
+ 'conditions' => array('NOT' => array('two >' => 3)),
+ 'limit' => 1
+ ));
+ $this->assertEqual($result['Post']['two'],2);
+
+ $dbo =& $Post->getDataSource();
+ $Post->virtualFields = array('other_field' => 'Post.id + 1');
+ $result = $Post->find('first',array(
+ 'conditions' => array('other_field' => 3),
+ 'limit' => 1
+ ));
+ $this->assertEqual($result['Post']['id'],2);
+
+ $Post->virtualFields = array('other_field' => 'Post.id + 1');
+ $result = $Post->find('all',array(
+ 'fields' => array($dbo->calculate($Post,'max',array('other_field')))
+ ));
+ $this->assertEqual($result[0][0]['other_field'],4);
+
ClassRegistry::flush();
$Writing = ClassRegistry::init(array('class' => 'Post', 'alias' => 'Writing'),'Model');
$Writing->virtualFields = array('two' => "1 + 1");
$result = $Writing->find('first');
$this->assertEqual($result['Writing']['two'],2);
+
+ $Post->create();
+ $Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
+ $result = $Post->field('other_field');
+ $this->assertEqual($result,4);
}
}
?>

0 comments on commit c65e2f3

Please sign in to comment.