Fixing virtual fields fetching for Model::field() calls

Adding doc blocks
This commit is contained in:
José Lorenzo Rodríguez 2009-11-12 00:35:08 -04:30
parent 22e36cb057
commit 7efafc685e
2 changed files with 33 additions and 1 deletions

View file

@ -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) {

View file

@ -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);
}
/**