Skip to content

Commit

Permalink
Extending virtual fields implementation to work on queries with field…
Browse files Browse the repository at this point in the history
… list
  • Loading branch information
lorenzo committed Dec 11, 2009
1 parent 5376531 commit a81edb6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
15 changes: 7 additions & 8 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -1789,10 +1789,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;
}
Expand Down Expand Up @@ -1905,11 +1907,8 @@ 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);
}
Expand Down
30 changes: 19 additions & 11 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit a81edb6

Please sign in to comment.