Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Order By in GroupModel select #26

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/GroupModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public function groupBy($group, $aggregate = [])

foreach ($aggregate as $field=>$expr) {


//$this->addField($field);
// field originally defined in the parent model
$field_object = $this->master_model->hasField($field); // use hasField here!

Expand Down Expand Up @@ -173,6 +175,9 @@ public function addGrouping(Query $query)
$query->group($this->expr($field));
}
}



}

/**
Expand Down Expand Up @@ -200,12 +205,7 @@ public function setLimit($count, $offset = null)
*
* @todo Incorrect implementation
*/
public function setOrder($field, $desc = null)
{
$this->master_model->setOrder($field, $desc);

return $this;
}

/**
* Set action.
Expand Down Expand Up @@ -253,6 +253,8 @@ public function action($mode, $args = [])

$this->hook('afterGroupSelect', [$query]);

$query = $this->orderByQuery($query);

return $query;

case 'count':
Expand Down Expand Up @@ -307,6 +309,32 @@ public function action($mode, $args = [])
return $query;
}

/**
* Applies Order By to the query
*
*/
function orderByQuery($query)
{

if ($this->order) {

foreach ($this->order as $o) {

if(is_string($o[1])){
$query->order($o[0]." ".$o[1]);
} else {
$query->order($o[0],$o[1]);
}
}
}

return $query;

}




/**
* Our own way applying conditions, where we use "having" for
* fields
Expand Down