aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace/ORM
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-07-22 21:36:42 +0200
committerThomas Lange <code@nerdmind.de>2021-07-22 21:43:00 +0200
commit8662f123d781a6df4eb5bd05b3556cca94fcf334 (patch)
treee5da1e3c347e59be72e704450687548a023035cd /core/namespace/ORM
parent0db1beaea6b849790aa0f1f4ecbf6439ce4f1104 (diff)
downloadblog-8662f123d781a6df4eb5bd05b3556cca94fcf334.tar.gz
blog-8662f123d781a6df4eb5bd05b3556cca94fcf334.tar.xz
blog-8662f123d781a6df4eb5bd05b3556cca94fcf334.zip
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.
Diffstat (limited to 'core/namespace/ORM')
-rw-r--r--core/namespace/ORM/Repositories/PageRepository.php3
-rw-r--r--core/namespace/ORM/Repositories/PostRepository.php3
-rw-r--r--core/namespace/ORM/Repository.php99
-rw-r--r--core/namespace/ORM/RepositorySearch.php103
4 files changed, 109 insertions, 99 deletions
diff --git a/core/namespace/ORM/Repositories/PageRepository.php b/core/namespace/ORM/Repositories/PageRepository.php
index 8c7c94b..993fb67 100644
--- a/core/namespace/ORM/Repositories/PageRepository.php
+++ b/core/namespace/ORM/Repositories/PageRepository.php
@@ -1,8 +1,11 @@
<?php
namespace ORM\Repositories;
use ORM\Repository;
+use ORM\RepositorySearch;
class PageRepository extends Repository {
+ use RepositorySearch;
+
public static function getTableName(): string { return 'page'; }
public static function getClassName(): string { return 'ORM\Entities\Page'; }
}
diff --git a/core/namespace/ORM/Repositories/PostRepository.php b/core/namespace/ORM/Repositories/PostRepository.php
index 400d596..24ab47f 100644
--- a/core/namespace/ORM/Repositories/PostRepository.php
+++ b/core/namespace/ORM/Repositories/PostRepository.php
@@ -1,8 +1,11 @@
<?php
namespace ORM\Repositories;
use ORM\Repository;
+use ORM\RepositorySearch;
class PostRepository extends Repository {
+ use RepositorySearch;
+
public static function getTableName(): string { return 'post'; }
public static function getClassName(): string { return 'ORM\Entities\Post'; }
}
diff --git a/core/namespace/ORM/Repository.php b/core/namespace/ORM/Repository.php
index 44bd62e..423a286 100644
--- a/core/namespace/ORM/Repository.php
+++ b/core/namespace/ORM/Repository.php
@@ -7,9 +7,6 @@ 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;
@@ -260,100 +257,4 @@ abstract class Repository {
return $this->fetchEntities($Statement);
}
-
- #===============================================================================
- # Get entities based on search query
- #===============================================================================
- public function search(string $search, array $filter = [], int $limit = NULL, int $offset = 0): array {
- if(strlen($filter['year'] ?? '') !== 0) {
- $extend[] = 'YEAR(time_insert) = ? AND';
- $params[] = $filter['year'];
- }
-
- if(strlen($filter['month'] ?? '') !== 0) {
- $extend[] = 'MONTH(time_insert) = ? AND';
- $params[] = $filter['month'];
- }
-
- if(strlen($filter['day'] ?? '') !== 0) {
- $extend[] = 'DAY(time_insert) = ? AND';
- $params[] = $filter['day'];
- }
-
- if($limit) {
- $limit = "LIMIT $offset,$limit";
- }
-
- $dateparts = implode(' ', $extend ?? []);
-
- $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]));
-
- 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 [];
- }
}
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 @@
+<?php
+namespace ORM;
+
+trait RepositorySearch {
+ # See "search" method for more details.
+ private $lastSearchOverallCount = 0;
+
+ #===============================================================================
+ # Get entities based on search query
+ #===============================================================================
+ public function search(string $search, array $filter = [], int $limit = NULL, int $offset = 0): array {
+ if(strlen($filter['year'] ?? '') !== 0) {
+ $extend[] = 'YEAR(time_insert) = ? AND';
+ $params[] = $filter['year'];
+ }
+
+ if(strlen($filter['month'] ?? '') !== 0) {
+ $extend[] = 'MONTH(time_insert) = ? AND';
+ $params[] = $filter['month'];
+ }
+
+ if(strlen($filter['day'] ?? '') !== 0) {
+ $extend[] = 'DAY(time_insert) = ? AND';
+ $params[] = $filter['day'];
+ }
+
+ if($limit) {
+ $limit = "LIMIT $offset,$limit";
+ }
+
+ $dateparts = implode(' ', $extend ?? []);
+
+ $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]));
+
+ 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 [];
+ }
+}