From 8662f123d781a6df4eb5bd05b3556cca94fcf334 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Thu, 22 Jul 2021 21:36:42 +0200 Subject: Move repository search functionality into a trait Move the methods for the search functionality of the abstract Repository class into a separate trait and use it in the Page and Post repository. The reason because of this is that only the Page and Post repositories having a search functionality, while the other repositories have not. --- core/namespace/ORM/RepositorySearch.php | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 core/namespace/ORM/RepositorySearch.php (limited to 'core/namespace/ORM/RepositorySearch.php') diff --git a/core/namespace/ORM/RepositorySearch.php b/core/namespace/ORM/RepositorySearch.php new file mode 100644 index 0000000..8a61998 --- /dev/null +++ b/core/namespace/ORM/RepositorySearch.php @@ -0,0 +1,103 @@ +Database->prepare($query); + $Statement->execute(array_merge($params ?? [], [$search])); + + 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; + } + + #=============================================================================== + # Get a list of distinct days + #=============================================================================== + public function getDistinctDays(): array { + $query = 'SELECT DISTINCT DAY(time_insert) AS d FROM %s ORDER BY d'; + $query = sprintf($query, static::getTableName()); + + $Statement = $this->Database->query($query); + + if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) { + return $result; + } + + return []; + } + + #=============================================================================== + # Get a list of distinct months + #=============================================================================== + public function getDistinctMonths(): array { + $query = 'SELECT DISTINCT MONTH(time_insert) AS m FROM %s ORDER BY m'; + $query = sprintf($query, static::getTableName()); + + $Statement = $this->Database->query($query); + + if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) { + return $result; + } + + return []; + } + + #=============================================================================== + # Get a list of distinct years + #=============================================================================== + public function getDistinctYears(): array { + $query = 'SELECT DISTINCT YEAR(time_insert) AS y FROM %s ORDER BY y'; + $query = sprintf($query, static::getTableName()); + + $Statement = $this->Database->query($query); + + if($result = $Statement->fetchAll($this->Database::FETCH_COLUMN)) { + return $result; + } + + return []; + } +} -- cgit v1.2.3