aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-07-17 19:28:50 +0200
committerThomas Lange <code@nerdmind.de>2021-07-17 19:43:35 +0200
commit24b01055b2afae222e4a12894feee6214598ece4 (patch)
tree7e987abe40ae8f975a6e9bb4ec3d098a5caa4571 /core/namespace
parent89e2ab9b872f34361bed8274f3d90cda6abddced (diff)
downloadblog-24b01055b2afae222e4a12894feee6214598ece4.tar.gz
blog-24b01055b2afae222e4a12894feee6214598ece4.tar.xz
blog-24b01055b2afae222e4a12894feee6214598ece4.zip
Reduce the redundant mapping logic in repositories
Reduce the redundant mapping logic in the repository classes by using the new methods "fetchEntity" and "fetchEntities".
Diffstat (limited to 'core/namespace')
-rw-r--r--core/namespace/ORM/Repositories/Category.php14
-rw-r--r--core/namespace/ORM/Repository.php68
2 files changed, 33 insertions, 49 deletions
diff --git a/core/namespace/ORM/Repositories/Category.php b/core/namespace/ORM/Repositories/Category.php
index ead54a4..c00fdf3 100644
--- a/core/namespace/ORM/Repositories/Category.php
+++ b/core/namespace/ORM/Repositories/Category.php
@@ -32,12 +32,7 @@ class Category extends Repository {
$Statement->execute([$value]);
# TODO: Virtual column _depth shall not be fetched into the entity class
- if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
- $this->storeMultipleInstances($entities);
- return $entities;
- }
-
- return [];
+ return $this->fetchEntities($Statement);
}
#===============================================================================
@@ -61,12 +56,7 @@ class Category extends Repository {
$Statement = $this->Database->prepare($query);
$Statement->execute();
- if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
- $this->storeMultipleInstances($entities);
- return $entities;
- }
-
- return [];
+ return $this->fetchEntities($Statement);
}
#===============================================================================
diff --git a/core/namespace/ORM/Repository.php b/core/namespace/ORM/Repository.php
index c0677a9..a1efa3c 100644
--- a/core/namespace/ORM/Repository.php
+++ b/core/namespace/ORM/Repository.php
@@ -1,6 +1,7 @@
<?php
namespace ORM;
use Database;
+use PDOStatement;
abstract class Repository {
protected $Database;
@@ -14,6 +15,30 @@ abstract class Repository {
}
#===============================================================================
+ # Fetch entity from a PDOStatement result
+ #===============================================================================
+ protected function fetchEntity(PDOStatement $Statement): ?EntityInterface {
+ if($Entity = $Statement->fetchObject(static::getClassName())) {
+ $this->storeInstance($Entity->getID(), $Entity);
+ return $Entity;
+ }
+
+ return NULL;
+ }
+
+ #===============================================================================
+ # Fetch multiple entities from a PDOStatement result
+ #===============================================================================
+ protected function fetchEntities(PDOStatement $Statement): array {
+ if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
+ $this->storeMultipleInstances($entities);
+ return $entities;
+ }
+
+ return [];
+ }
+
+ #===============================================================================
# Adds an entity to the runtime cache
#===============================================================================
protected function storeInstance(int $identifier, EntityInterface $Entity) {
@@ -119,12 +144,7 @@ abstract class Repository {
$Statement = $this->Database->prepare($query);
$Statement->execute([$value]);
- if($Entity = $Statement->fetchObject(static::getClassName())) {
- $this->storeInstance($Entity->getID(), $Entity);
- return $Entity;
- }
-
- return NULL;
+ return $this->fetchEntity($Statement);
}
#===============================================================================
@@ -137,12 +157,7 @@ abstract class Repository {
$Statement = $this->Database->prepare($query);
$Statement->execute([$Entity->get('time_insert')]);
- if($Entity = $Statement->fetchObject(static::getClassName())) {
- $this->storeInstance($Entity->getID(), $Entity);
- return $Entity;
- }
-
- return NULL;
+ return $this->fetchEntity($Statement);
}
#===============================================================================
@@ -155,12 +170,7 @@ abstract class Repository {
$Statement = $this->Database->prepare($query);
$Statement->execute([$Entity->get('time_insert')]);
- if($Entity = $Statement->fetchObject(static::getClassName())) {
- $this->storeInstance($Entity->getID(), $Entity);
- return $Entity;
- }
-
- return NULL;
+ return $this->fetchEntity($Statement);
}
#===========================================================================
@@ -171,13 +181,7 @@ abstract class Repository {
$query = sprintf($query, static::getTableName());
$Statement = $this->Database->query($query);
-
- if($Entity = $Statement->fetchObject(static::getClassName())) {
- $this->storeInstance($Entity->getID(), $Entity);
- return $Entity;
- }
-
- return NULL;
+ return $this->fetchEntity($Statement);
}
#===========================================================================
@@ -251,12 +255,7 @@ abstract class Repository {
$Statement = $this->Database->prepare($query);
$Statement->execute($params);
- if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
- $this->storeMultipleInstances($entities);
- return $entities;
- }
-
- return [];
+ return $this->fetchEntities($Statement);
}
#===============================================================================
@@ -291,12 +290,7 @@ abstract class Repository {
$Statement = $this->Database->prepare($query);
$Statement->execute(array_merge($params ?? [], [$search]));
- if($entities = $Statement->fetchAll($this->Database::FETCH_CLASS, static::getClassName())) {
- $this->storeMultipleInstances($entities);
- return $entities;
- }
-
- return [];
+ return $this->fetchEntities($Statement);
}
#===============================================================================