Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extending virtual fields implementation to work on queries with field…

… list
  • Loading branch information...
commit 00c0bb4d62467740cd2ff8fc71120a372ea353db 1 parent 19c91f1
@lorenzo lorenzo authored
View
26 cake/libs/model/datasources/dbo_source.php
@@ -1758,10 +1758,12 @@ function __scrubQueryData($data) {
return $data;
}
- function _constructVirtualFields(&$model,$fields) {
+ function _constructVirtualFields(&$model,$alias,$fields) {
$virtual = array();
- foreach ($fields as $name => $expression) {
- $virtual[] = $expression . " {$this->alias} {$model->alias}__{$name}";
+ foreach ($fields as $field) {
+ $virtualField = $this->name("{$alias}__{$field}");
+ $expression = $model->virtualFields[$field];
+ $virtual[] = $expression . " {$this->alias} {$virtualField}";
}
return $virtual;
}
@@ -1790,10 +1792,19 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
if (!$quote) {
return $fields;
}
+ $virtual = array();
+ if (!empty($model->virtualFields)) {
+ $keys = array_keys($model->virtualFields);
+ $virtual = ($allFields) ? $keys : array_intersect($keys,$fields);
+ }
$count = count($fields);
if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
for ($i = 0; $i < $count; $i++) {
+ if (in_array($fields[$i],$virtual)) {
+ unset($fields[$i]);
+ continue;
+ }
if (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])){
continue;
} elseif (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
@@ -1847,12 +1858,9 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
}
}
}
-
- if (!empty($model->virtualFields)) {
- if ($allFields) {
- $fields = array_merge($fields,$this->_constructVirtualFields($model,$model->virtualFields));
- } else {
- }
+
+ if (!empty($virtual)) {
+ $fields = array_merge($fields,$this->_constructVirtualFields($model,$alias,$virtual));
}
return array_unique($fields);
}
View
30 cake/tests/cases/libs/model/model_read.test.php
@@ -27,19 +27,27 @@
class ModelReadTest extends BaseModelTest {
function testVirtualFields() {
- $this->loadFixtures('Post');
+ $this->loadFixtures('Post','Author');
$Post = ClassRegistry::init('Post');
$Post->virtualFields = array('two' => "1 + 1");
- $expected = array(
- 'author_id' => 1,
- 'title' => 'First Post',
- 'body' => 'First Post Body',
- 'published' => 'Y',
- 'created' => '2007-03-18 10:39:23',
- 'updated' => '2007-03-18 10:41:31'
- );
- debug($Post->find('first'));
- exit;
+ $result = $Post->find('first');
+ $this->assertEqual($result['Post']['two'],2);
+
+ $Post->Author->virtualFields = array('false' => '1 = 2');
+ $result = $Post->find('first');
+ $this->assertEqual($result['Post']['two'],2);
+ $this->assertEqual($result['Author']['false'],false);
+
+ $result = $Post->find('first',array('fields' => array('author_id')));
+ $this->assertFalse(isset($result['Post']['two']));
+ $this->assertFalse(isset($result['Author']['false']));
+
+ $result = $Post->find('first',array('fields' => array('author_id','two')));
+ $this->assertEqual($result['Post']['two'],2);
+ $this->assertFalse(isset($result['Author']['false']));
+
+ $result = $Post->find('first',array('fields' => array('two')));
+ $this->assertEqual($result['Post']['two'],2);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.