From bb2c26ac56e5b4fdcde728fa50daa2f05bb96507 Mon Sep 17 00:00:00 2001 From: James Pluck Date: Thu, 12 Jun 2014 11:31:27 +1200 Subject: [PATCH] FIX Total Items count on Search This issue causes pagination to fail for search results on sites using the postgres db. Patch corrects the TotalItems in the returned List to show all items matching the search criteria rather than just the items returned in the current 'page' requested. --- code/PostgreSQLDatabase.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/code/PostgreSQLDatabase.php b/code/PostgreSQLDatabase.php index 2a9dc08..a27708e 100644 --- a/code/PostgreSQLDatabase.php +++ b/code/PostgreSQLDatabase.php @@ -1885,12 +1885,14 @@ class PostgreSQLDatabase extends SS_Database { $fullQuery = "SELECT * FROM (" . implode(" UNION ", $tables) . ") AS q1 $orderBy LIMIT $limit OFFSET $offset"; + // Get the total items in this search + $totalItemsQuery = "SELECT COUNT(*) AS totalitems FROM (" . implode(" UNION ", $tables) . ") AS q1"; + $totalCount = DB::query($totalItemsQuery); + // Get records $records = DB::query($fullQuery); - $totalCount=0; foreach($records as $record){ $objects[] = new $record['ClassName']($record); - $totalCount++; } if(isset($objects)) $results = new ArrayList($objects); @@ -1899,7 +1901,7 @@ class PostgreSQLDatabase extends SS_Database { $list->setLimitItems(false); $list->setPageStart($start); $list->setPageLength($pageLength); - $list->setTotalItems($totalCount); + $list->setTotalItems($totalCount->value()); return $list; }