diff options
author | Thomas Lange <code@nerdmind.de> | 2021-07-22 21:04:02 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-07-22 21:04:02 +0200 |
commit | cb9097e0199cf6afca3f144edb7d7addaec1cb59 (patch) | |
tree | b4aff017f6a8c22b4b32c8316f4ae3b887bcbd3a /core/namespace/ORM/Repository.php | |
parent | ae38025d1d03ef3fa12c73b8ea48af3df02e2351 (diff) | |
download | blog-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/ORM/Repository.php')
-rw-r--r-- | core/namespace/ORM/Repository.php | 24 |
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; } #=============================================================================== |