aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-07-22 21:04:02 +0200
committerThomas Lange <code@nerdmind.de>2021-07-22 21:04:02 +0200
commitcb9097e0199cf6afca3f144edb7d7addaec1cb59 (patch)
treeb4aff017f6a8c22b4b32c8316f4ae3b887bcbd3a /core/namespace
parentae38025d1d03ef3fa12c73b8ea48af3df02e2351 (diff)
downloadblog-cb9097e0199cf6afca3f144edb7d7addaec1cb59.tar.gz
blog-cb9097e0199cf6afca3f144edb7d7addaec1cb59.tar.xz
blog-cb9097e0199cf6afca3f144edb7d7addaec1cb59.zip
Add pagination for search results
Add pagination for search results in the admin and default theme.
Diffstat (limited to 'core/namespace')
-rw-r--r--core/namespace/ORM/Repository.php24
1 files changed, 18 insertions, 6 deletions
diff --git a/core/namespace/ORM/Repository.php b/core/namespace/ORM/Repository.php
index b2778f4..44bd62e 100644
--- a/core/namespace/ORM/Repository.php
+++ b/core/namespace/ORM/Repository.php
@@ -7,6 +7,9 @@ abstract class Repository {
protected $Database;
protected $entities = [];
+ # See "search" method for more details.
+ private $lastSearchOverallCount = 0;
+
abstract public static function getTableName(): string;
abstract public static function getClassName(): string;
@@ -262,10 +265,6 @@ abstract class Repository {
# Get entities based on search query
#===============================================================================
public function search(string $search, array $filter = [], int $limit = NULL, int $offset = 0): array {
- if($search === '*') {
- return $this->getAll([], NULL, 20);
- }
-
if(strlen($filter['year'] ?? '') !== 0) {
$extend[] = 'YEAR(time_insert) = ? AND';
$params[] = $filter['year'];
@@ -287,14 +286,27 @@ abstract class Repository {
$dateparts = implode(' ', $extend ?? []);
- $query = 'SELECT * FROM %s WHERE %s MATCH(name, body)
+ $query = 'SELECT *, COUNT(*) OVER() AS _count FROM %s WHERE %s MATCH(name, body)
AGAINST(? IN BOOLEAN MODE) %s';
$query = sprintf($query, static::getTableName(), $dateparts, $limit ?? 'LIMIT 20');
$Statement = $this->Database->prepare($query);
$Statement->execute(array_merge($params ?? [], [$search]));
- return $this->fetchEntities($Statement);
+ if($entities = $this->fetchEntities($Statement)) {
+ # Temporary (maybe crappy) solution to prevent a second count query.
+ # Virtual column "_count" does not belong into the entities.
+ $this->lastSearchOverallCount = $entities[0]->get('_count');
+ }
+
+ return $entities;
+ }
+
+ #===============================================================================
+ # Get the number of overall results for the last performed search
+ #===============================================================================
+ public function getLastSearchOverallCount(): int {
+ return $this->lastSearchOverallCount;
}
#===============================================================================