Additional tests for virtualFields and fixing issues with index 0 being removed. Fixes #208

This commit is contained in:
Mark Story 2010-01-21 10:35:20 -05:00
parent 570c9e5304
commit 913450daef
3 changed files with 39 additions and 11 deletions

View file

@ -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);
$virtual = ($allFields) ? $virtualKeys : array_intersect($virtualKeys, $fields);
foreach ($virtual as $i => $field) {
if (strpos($field, '.') !== false) {
$virtual[$i] = str_replace($model->alias . '.', '', $field);
}
foreach($virtual as &$field) {
if (strpos($field, '.')) {
$field = str_replace($model->alias . '.', '', $field);
$fields = array_diff($fields, array($model->alias . '.' . $field));
$fields = array_diff($fields, array($field));
}
$fields = array_values($fields);
}
$count = count($fields);

View file

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

View file

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