aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace/Item.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/namespace/Item.php')
-rw-r--r--core/namespace/Item.php30
1 files changed, 29 insertions, 1 deletions
diff --git a/core/namespace/Item.php b/core/namespace/Item.php
index 02de382..0a7d364 100644
--- a/core/namespace/Item.php
+++ b/core/namespace/Item.php
@@ -10,7 +10,7 @@ abstract class Item implements ItemInterface {
#===============================================================================
# Abstract item constructor
#===============================================================================
- public final function __construct($itemID, \Database $Database) {
+ public final function __construct($param, \Database $Database) {
$this->Database = $Database;
$this->Reflection = new ReflectionObject($this);
@@ -18,6 +18,17 @@ abstract class Item implements ItemInterface {
$attribute = "{$this->Reflection->getNamespaceName()}\\Attribute";
$exception = "{$this->Reflection->getNamespaceName()}\\Exception";
+ # If $param is an instance of Attribute,
+ # skip fetching attribute from database!
+ if($param instanceof Attribute) {
+ $this->Attribute = $param;
+ return;
+ }
+
+ # If this gets executed, then $param
+ # is not an instance of Attribute!
+ $itemID = $param;
+
#===============================================================================
# Checking if item in database exists
#===============================================================================
@@ -168,4 +179,21 @@ abstract class Item implements ItemInterface {
return 0;
}
+
+ #===============================================================================
+ # Return Attribute instance based on field comparison with value
+ #===============================================================================
+ public static function getByField($field, $value, \Database $Database): Attribute {
+ $exception = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Exception';
+ $attribute = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Attribute';
+ $Statement = $Database->prepare('SELECT * FROM '.$attribute::TABLE." WHERE {$field} = ?");
+
+ if($Statement->execute([$value])) {
+ if(!$Attribute = $Statement->fetchObject($attribute)) {
+ throw new $exception(sprintf("Item with value %s on field %s does not exist.", $value, $field));
+ }
+
+ return $Attribute;
+ }
+ }
}