Skip to content
Permalink
Browse files

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

…ng removed. Fixes #208
  • Loading branch information...
markstory committed Jan 21, 2010
1 parent 570c9e5 commit 913450daefb39114a4264b4cb56285c4ec669d16
@@ -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);
}
@@ -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);
}
/**
@@ -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']));
}
/**

0 comments on commit 913450d

Please sign in to comment.
You can’t perform that action at this time.