Skip to content

Commit

Permalink
Fixing virtual fields fetching for Model::field() calls
Browse files Browse the repository at this point in the history
Adding doc blocks
  • Loading branch information
lorenzo committed Dec 11, 2009
1 parent 22e36cb commit 7efafc6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cake/libs/model/datasources/dbo_source.php
Expand Up @@ -369,7 +369,6 @@ function fetchAll($sql, $cache = true, $modelName = null) {

$first = $this->fetchRow();
if ($first != null) {
$this->fetchVirtualField($first);
$out[] = $first;
}
while ($this->hasResult() && $item = $this->fetchResult()) {
Expand All @@ -391,6 +390,12 @@ function fetchAll($sql, $cache = true, $modelName = null) {
}
}

/**
* Modifies $result array to place virtual fields in model entry where they belongs to
*
* @param array $resut REference to the fetched row
* @return void
*/
function fetchVirtualField(&$result) {
if (isset($result[0]) && is_array($result[0])) {
foreach ($result[0] as $field => $value) {
Expand Down Expand Up @@ -1789,6 +1794,14 @@ function __scrubQueryData($data) {
return $data;
}

/**
* Converts model virtual fields into sql expressions to be fetched later
*
* @param Model $model
* @param string $alias Alias tablename
* @param mixed $fields virtual fields to be used on query
* @return array
*/
function _constructVirtualFields(&$model,$alias,$fields) {
$virtual = array();
foreach ($fields as $field) {
Expand Down
19 changes: 19 additions & 0 deletions cake/tests/cases/libs/model/model_read.test.php
Expand Up @@ -26,6 +26,15 @@
*/
class ModelReadTest extends BaseModelTest {

/**
* testVirtualFields()
*
* Test correct fetching of virtual fields
* currently is not possible to do Relation.virtualField
*
* @access public
* @return void
*/
function testVirtualFields() {
$this->loadFixtures('Post','Author');
$Post = ClassRegistry::init('Post');
Expand All @@ -48,6 +57,16 @@ function testVirtualFields() {

$result = $Post->find('first',array('fields' => array('two')));
$this->assertEqual($result['Post']['two'],2);

$Post->id = 1;
$result = $Post->field('two');
$this->assertEqual($result,2);

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);
}

/**
Expand Down

0 comments on commit 7efafc6

Please sign in to comment.