Extending virtual fields implementation to work on queries with field list

This commit is contained in:
José Lorenzo Rodríguez 2009-11-11 23:12:03 -04:30
parent 19c91f1f70
commit 00c0bb4d62
2 changed files with 36 additions and 20 deletions

View file

@ -1758,10 +1758,12 @@ class DboSource extends DataSource {
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 @@ class DboSource extends DataSource {
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 @@ class DboSource extends DataSource {
}
}
}
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 file

@ -27,19 +27,27 @@ require_once dirname(__FILE__) . DS . 'model.test.php';
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);
}
/**