diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index c0d86f10f..f4129e608 100755 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -1877,18 +1877,19 @@ class DboSource extends DataSource { } $virtual = array(); $virtualFields = $model->getVirtualField(); - if ($virtualFields) { - $keys = array_keys($virtualFields); - foreach($keys as $field) { - $keys[] = $model->alias . '.' . $field; + if (!empty($virtualFields)) { + $virtualKeys = array_keys($virtualFields); + foreach ($virtualKeys as $field) { + $virtualKeys[] = $model->alias . '.' . $field; } - $virtual = ($allFields) ? $keys : array_intersect($keys, $fields); - } - foreach($virtual as &$field) { - if (strpos($field, '.')) { - $field = str_replace($model->alias . '.', '', $field); - $fields = array_diff($fields, array($model->alias . '.' . $field)); + $virtual = ($allFields) ? $virtualKeys : array_intersect($virtualKeys, $fields); + foreach ($virtual as $i => $field) { + if (strpos($field, '.') !== false) { + $virtual[$i] = str_replace($model->alias . '.', '', $field); + } + $fields = array_diff($fields, array($field)); } + $fields = array_values($fields); } $count = count($fields); @@ -1954,7 +1955,7 @@ class DboSource extends DataSource { } } if (!empty($virtual)) { - $fields = array_merge($fields,$this->_constructVirtualFields($model, $alias, $virtual)); + $fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual)); } return array_unique($fields); } diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index 9a7c79baa..0bd4a52b4 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -4142,6 +4142,13 @@ class DboSourceTest extends CakeTestCase { '(NOW()) AS `Article__this_moment`', ); $this->assertEqual($expected, $result); + + $result = $this->db->fields($Article, null, array('Article.this_moment', 'Article.title')); + $expected = array( + '`Article`.`title`', + '(NOW()) AS `Article__this_moment`', + ); + $this->assertEqual($expected, $result); } /** diff --git a/cake/tests/cases/libs/model/model_read.test.php b/cake/tests/cases/libs/model/model_read.test.php index e75ef796c..83c9cd060 100755 --- a/cake/tests/cases/libs/model/model_read.test.php +++ b/cake/tests/cases/libs/model/model_read.test.php @@ -7249,6 +7249,26 @@ class ModelReadTest extends BaseModelTest { )); $this->assertEqual($result, $expectation); + + + $Author =& ClassRegistry::init('Author'); + $Author->virtualFields = array( + 'full_name' => 'CONCAT(Author.user, " ", Author.id)' + ); + + $result = $Author->find('first', array( + 'conditions' => array('Author.user' => 'mariano'), + 'fields' => array('Author.password', 'Author.full_name'), + 'recursive' => -1 + )); + $this->assertTrue(isset($result['Author']['full_name'])); + + $result = $Author->find('first', array( + 'conditions' => array('Author.user' => 'mariano'), + 'fields' => array('Author.full_name', 'Author.password'), + 'recursive' => -1 + )); + $this->assertTrue(isset($result['Author']['full_name'])); } /**