Skip to content

Commit

Permalink
Update DboSource::group():
Browse files Browse the repository at this point in the history
* Update documentation.
* Add Model type hint.
* Bail early when fields are empty.
  • Loading branch information
bar committed Dec 10, 2013
1 parent 5c595b9 commit 63a192e
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -2870,26 +2870,32 @@ public function order($keys, $direction = 'ASC', $model = null) {
}

/**
* Create a GROUP BY SQL clause
* Create a GROUP BY SQL clause.
*
* @param string $group Group By Condition
* @param Model $model
* @return string string condition or null
* @param string|array $fields Group By fields
* @param Model $Model
* @return string Group By clause or null.
*/
public function group($group, $model = null) {
if ($group) {
if (!is_array($group)) {
$group = array($group);
}
foreach ($group as $index => $key) {
if (is_object($model) && $model->isVirtualField($key)) {
$group[$index] = '(' . $model->getVirtualField($key) . ')';
public function group($fields, Model $Model = null) {
if (empty($fields)) {
return null;
}

if (!is_array($fields)) {
$fields = array($fields);
}

if (!empty($Model)) {
foreach ($fields as $index => $key) {
if ($Model->isVirtualField($key)) {
$fields[$index] = '(' . $Model->getVirtualField($key) . ')';
}
}
$group = implode(', ', $group);
return ' GROUP BY ' . $this->_quoteFields($group);
}
return null;

$fields = implode(', ', $fields);

return ' GROUP BY ' . $this->_quoteFields($fields);
}

/**
Expand Down

0 comments on commit 63a192e

Please sign in to comment.