Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

Adding doc blocks
  • Loading branch information...
commit ec672e99e817771216a9a6135fae9eafdff5b77f 1 parent 4685daf
@lorenzo lorenzo authored
View
19 cake/libs/model/datasources/dbo_source.php
@@ -340,6 +340,9 @@ function fetchRow($sql = null) {
if ($this->hasResult()) {
$this->resultSet($this->_result);
$resultRow = $this->fetchResult();
+ if (!empty($resultRow)) {
+ $this->fetchVirtualField($resultRow);
+ }
return $resultRow;
} else {
return null;
@@ -366,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()) {
@@ -388,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) {
@@ -420,7 +428,6 @@ function fetchVirtualField(&$result) {
*/
function field($name, $sql) {
$data = $this->fetchRow($sql);
-
if (!isset($data[$name]) || empty($data[$name])) {
return false;
} else {
@@ -1758,6 +1765,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) {
View
19 cake/tests/cases/libs/model/model_read.test.php
@@ -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');
@@ -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);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.