From 7efafc685e743000176ba29fc928cf7682991274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Thu, 12 Nov 2009 00:35:08 -0430 Subject: [PATCH] Fixing virtual fields fetching for Model::field() calls Adding doc blocks --- cake/libs/model/datasources/dbo_source.php | 15 ++++++++++++++- .../cases/libs/model/model_read.test.php | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index a7aded729..db6727cd5 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -369,7 +369,6 @@ class DboSource extends DataSource { $first = $this->fetchRow(); if ($first != null) { - $this->fetchVirtualField($first); $out[] = $first; } while ($this->hasResult() && $item = $this->fetchResult()) { @@ -391,6 +390,12 @@ class DboSource extends DataSource { } } +/** + * 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) { @@ -1789,6 +1794,14 @@ class DboSource extends DataSource { 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) { diff --git a/cake/tests/cases/libs/model/model_read.test.php b/cake/tests/cases/libs/model/model_read.test.php index 97fcfbc76..6d53b0996 100644 --- a/cake/tests/cases/libs/model/model_read.test.php +++ b/cake/tests/cases/libs/model/model_read.test.php @@ -26,6 +26,15 @@ require_once dirname(__FILE__) . DS . 'model.test.php'; */ 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'); @@ -48,6 +57,16 @@ class ModelReadTest extends BaseModelTest { $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); } /**