mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Added virtual field support for GROUP BY.
This commit is contained in:
parent
dda2414b5b
commit
bbb105fc8c
2 changed files with 30 additions and 2 deletions
9
cake/libs/model/datasources/dbo_source.php
Normal file → Executable file
9
cake/libs/model/datasources/dbo_source.php
Normal file → Executable file
|
@ -1413,7 +1413,7 @@ class DboSource extends DataSource {
|
|||
'order' => $this->order($query['order'], 'ASC', $model),
|
||||
'limit' => $this->limit($query['limit'], $query['offset']),
|
||||
'joins' => implode(' ', $query['joins']),
|
||||
'group' => $this->group($query['group'])
|
||||
'group' => $this->group($query['group'], $model)
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -2328,9 +2328,14 @@ class DboSource extends DataSource {
|
|||
* @return mixed string condition or null
|
||||
* @access public
|
||||
*/
|
||||
function group($group) {
|
||||
function group($group, &$model = null) {
|
||||
if ($group) {
|
||||
if (is_array($group)) {
|
||||
foreach($group as $index => $key) {
|
||||
if ($model->isVirtualField($key)) {
|
||||
$group[$index] = '(' . $model->getVirtualField($key) . ')';
|
||||
}
|
||||
}
|
||||
$group = implode(', ', $group);
|
||||
}
|
||||
return ' GROUP BY ' . $this->__quoteFields($group);
|
||||
|
|
23
cake/tests/cases/libs/model/model_read.test.php
Normal file → Executable file
23
cake/tests/cases/libs/model/model_read.test.php
Normal file → Executable file
|
@ -7240,6 +7240,29 @@ class ModelReadTest extends BaseModelTest {
|
|||
$Post->virtualFields = array('other_field' => 'COUNT(Post.id) + 1');
|
||||
$result = $Post->field('other_field');
|
||||
$this->assertEqual($result, 4);
|
||||
|
||||
ClassRegistry::flush();
|
||||
$Post = ClassRegistry::init('Post');
|
||||
|
||||
$Post->create();
|
||||
$Post->virtualFields = array(
|
||||
'year' => 'YEAR(Post.created)',
|
||||
'unique_test_field' => 'COUNT(Post.id)'
|
||||
);
|
||||
|
||||
$expectation = array(
|
||||
'Post' => array(
|
||||
'year' => 2007,
|
||||
'unique_test_field' => 3
|
||||
)
|
||||
);
|
||||
|
||||
$result = $Post->find('first', array(
|
||||
'fields' => array_keys($Post->virtualFields),
|
||||
'group' => array('year')
|
||||
));
|
||||
|
||||
$this->assertEqual($result, $expectation);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue