Initial implementation of model virtual fields

This commit is contained in:
José Lorenzo Rodríguez 2009-11-10 01:08:07 -04:30
parent df151395c5
commit 19c91f1f70
2 changed files with 58 additions and 1 deletions

View file

@ -366,9 +366,11 @@ class DboSource extends DataSource {
$first = $this->fetchRow(); $first = $this->fetchRow();
if ($first != null) { if ($first != null) {
$this->fetchVirtualField($first);
$out[] = $first; $out[] = $first;
} }
while ($this->hasResult() && $item = $this->fetchResult()) { while ($this->hasResult() && $item = $this->fetchResult()) {
$this->fetchVirtualField($item);
$out[] = $item; $out[] = $item;
} }
@ -386,6 +388,29 @@ class DboSource extends DataSource {
} }
} }
function fetchVirtualField(&$result) {
if (isset($result[0]) && is_array($result[0])) {
foreach ($result[0] as $field => $value) {
if (strpos($field,'__') === false) {
continue;
}
list($alias,$virtual) = explode('__',$field);
if (!ClassRegistry::isKeySet($alias)) {
retrun;
}
$model = ClassRegistry::getObject($alias);
if (isset($model->virtualFields[$virtual])) {
$result[$alias][$virtual] = $value;
unset($result[0][$field]);
}
}
if (empty($result[0])) {
unset($result[0]);
}
}
}
/** /**
* Returns a single field of the first of query results for a given SQL query, or false if empty. * Returns a single field of the first of query results for a given SQL query, or false if empty.
* *
@ -1733,6 +1758,14 @@ class DboSource extends DataSource {
return $data; return $data;
} }
function _constructVirtualFields(&$model,$fields) {
$virtual = array();
foreach ($fields as $name => $expression) {
$virtual[] = $expression . " {$this->alias} {$model->alias}__{$name}";
}
return $virtual;
}
/** /**
* Generates the fields list of an SQL query. * Generates the fields list of an SQL query.
* *
@ -1746,7 +1779,8 @@ class DboSource extends DataSource {
if (empty($alias)) { if (empty($alias)) {
$alias = $model->alias; $alias = $model->alias;
} }
if (empty($fields)) { $allFields = empty($fields);
if ($allFields) {
$fields = array_keys($model->schema()); $fields = array_keys($model->schema());
} elseif (!is_array($fields)) { } elseif (!is_array($fields)) {
$fields = String::tokenize($fields); $fields = String::tokenize($fields);
@ -1813,6 +1847,13 @@ class DboSource extends DataSource {
} }
} }
} }
if (!empty($model->virtualFields)) {
if ($allFields) {
$fields = array_merge($fields,$this->_constructVirtualFields($model,$model->virtualFields));
} else {
}
}
return array_unique($fields); return array_unique($fields);
} }

View file

@ -26,6 +26,22 @@ require_once dirname(__FILE__) . DS . 'model.test.php';
*/ */
class ModelReadTest extends BaseModelTest { class ModelReadTest extends BaseModelTest {
function testVirtualFields() {
$this->loadFixtures('Post');
$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;
}
/** /**
* testFetchingNonUniqueFKJoinTableRecords() * testFetchingNonUniqueFKJoinTableRecords()
* *