Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Additional tests for virtualFields and fixing issues with index 0 bei…

…ng removed. Fixes #208
  • Loading branch information...
commit 913450daefb39114a4264b4cb56285c4ec669d16 1 parent 570c9e5
@markstory markstory authored
View
23 cake/libs/model/datasources/dbo_source.php
@@ -1877,18 +1877,19 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
}
$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 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
}
}
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);
}
View
7 cake/tests/cases/libs/model/datasources/dbo_source.test.php
@@ -4142,6 +4142,13 @@ function testVirtualFields() {
'(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);
}
/**
View
20 cake/tests/cases/libs/model/model_read.test.php
@@ -7249,6 +7249,26 @@ function testVirtualFields() {
));
$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']));
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.