mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Additional tests for virtualFields and fixing issues with index 0 being removed. Fixes #208
This commit is contained in:
parent
570c9e5304
commit
913450daef
3 changed files with 39 additions and 11 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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']));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue