diff options
author | Thomas Lange <code@nerdmind.de> | 2021-06-22 01:18:02 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-06-22 01:46:42 +0200 |
commit | 8cd1105b111b89106f24c5b50795afb5ff28a935 (patch) | |
tree | c789faac9de4d83cb409c45779e76be8938a4d66 /core/namespace/Item.php | |
parent | 7937df540b7d70b2bb87797442d0a0a0df197133 (diff) | |
download | blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.gz blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.xz blog-8cd1105b111b89106f24c5b50795afb5ff28a935.zip |
Implement new Repository and Entity classes
This commit adds new Repository and Entity classes which are better
abstracted from the rest of the application. They dont know anymore
about configuration options or how to parse to HTML because this is
not the job for the ORM but for other parts of the application.
The previous commits were a preparation for this big change.
An entity now represents just a single record from a specific table
of the database – nothing more. The repositories job is it to fetch
or update records of the database and instantiate the entities.
Another problem that was solved is the high amount of database queries
that was needed before. For example, on the blogs home page first were
all 10 latest post IDs fetched from the database and then another query
was executed with "WHERE id = :id" for *each* single post?! ...
This problem is solved with the new repository classes; they now use a
single query to fetch and build the entities of the 10 latest posts.
This change also solves the problem with database queries spread across
the application and limits the exzessive use of try/catch blocks which
were used before. The new classes make the whole code much cleaner. :)
Diffstat (limited to 'core/namespace/Item.php')
-rw-r--r-- | core/namespace/Item.php | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/core/namespace/Item.php b/core/namespace/Item.php deleted file mode 100644 index 453a3e6..0000000 --- a/core/namespace/Item.php +++ /dev/null @@ -1,98 +0,0 @@ -<?php -abstract class Item implements ItemInterface { - protected $Database = NULL; - protected $Attribute = NULL; - protected $Reflection = NULL; - - #=============================================================================== - # Abstract item constructor - #=============================================================================== - public final function __construct($itemID, \Database $Database) { - $this->Database = $Database; - - $this->Reflection = new ReflectionObject($this); - - $attribute = "{$this->Reflection->getNamespaceName()}\\Attribute"; - $exception = "{$this->Reflection->getNamespaceName()}\\Exception"; - - #=============================================================================== - # Checking if item in database exists - #=============================================================================== - $Statement = $Database->prepare(sprintf('SELECT * FROM %s WHERE id = ?', $attribute::TABLE)); - $Statement->execute([$itemID]); - - #=============================================================================== - # Checking if retrieving data failed - #=============================================================================== - if(!$this->Attribute = $Statement->fetchObject($attribute)) { - throw new $exception(sprintf('%s\\Item with ID %s does not exist', $this->Reflection->getNamespaceName(), (int) $itemID)); - } - } - - #=============================================================================== - # Return attribute by name (short hand wrapper) - #=============================================================================== - public function get($attribute) { - return $this->Attribute->get($attribute); - } - - #=============================================================================== - # Return Attribute object - #=============================================================================== - public final function getAttribute(): Attribute { - return $this->Attribute; - } - - #=============================================================================== - # Return unique ID - #=============================================================================== - public final function getID(): int { - return $this->Attribute->get('id'); - } - - #=============================================================================== - # Return previous item ID - #=============================================================================== - public function getPrevID(): int { - $execute = 'SELECT id FROM %s WHERE time_insert < ? ORDER BY time_insert DESC LIMIT 1'; - - $attribute = "{$this->Reflection->getNamespaceName()}\\Attribute"; - $Statement = $this->Database->prepare(sprintf($execute, $attribute::TABLE)); - - if($Statement->execute([$this->Attribute->get('time_insert')])) { - return $Statement->fetchColumn(); - } - - return 0; - } - - #=============================================================================== - # Return next item ID - #=============================================================================== - public function getNextID(): int { - $execute = 'SELECT id FROM %s WHERE time_insert > ? ORDER BY time_insert ASC LIMIT 1'; - - $attribute = "{$this->Reflection->getNamespaceName()}\\Attribute"; - $Statement = $this->Database->prepare(sprintf($execute, $attribute::TABLE)); - - if($Statement->execute([$this->Attribute->get('time_insert')])) { - return $Statement->fetchColumn(); - } - - return 0; - } - - #=============================================================================== - # Return unique ID based on specific field comparison with value - #=============================================================================== - public static function getIDByField($field, $value, \Database $Database): int { - $attribute = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Attribute'; - $Statement = $Database->prepare('SELECT id FROM '.$attribute::TABLE." WHERE {$field} = ?"); - - if($Statement->execute([$value])) { - return $Statement->fetchColumn(); - } - - return 0; - } -} |