Skip to content

Commit

Permalink
Paginate with Order by fix (#280)
Browse files Browse the repository at this point in the history
Fixed the issue that occured when the pagination was used together with Order By.
  • Loading branch information
Gaba93 authored and Mulkave committed Mar 13, 2018
1 parent 5f7b8ee commit 8572c3e
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ public function getBindings()
return $bindings;
}

/**
* Removes the order by clause when counting for the paginator.
* @author Gaba93
*/
private function backupFieldsForCount() {
$this->orders_backup = $this->orders;
$this->orders = null;
}

/**
* Readds the order clause.
* @author Gaba93
*/
private function restoreFieldsForCount() {
$this->orders = $this->orders_backup;
$this->orders_backup = null;
}

/**
* Get the count of the total records for the paginator.
*
Expand All @@ -207,17 +225,16 @@ public function getBindings()
*/
public function getCountForPagination($columns = ['*'])
{
// if I comment this paginate will work
// $this->backupFieldsForCount();
$this->backupFieldsForCount();

$this->aggregate = ['function' => 'count', 'columns' => $columns];


$results = $this->get();

$this->aggregate = null;

// if I comment this paginate will work
// $this->restoreFieldsForCount();
$this->restoreFieldsForCount();

if (isset($this->groups)) {
return count($results);
Expand Down

0 comments on commit 8572c3e

Please sign in to comment.