aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2018-04-01 15:15:49 +0200
committerThomas Lange <code@nerdmind.de>2018-04-01 15:15:49 +0200
commit7c307baeb1172ca1387966338d11ede4ee6dbb56 (patch)
tree2ec24a76e166b554250999b1df818e5a030459df
parent0a49f259b687aba010dd2edfcdf73d040622b561 (diff)
downloadblog-7c307baeb1172ca1387966338d11ede4ee6dbb56.tar.gz
blog-7c307baeb1172ca1387966338d11ede4ee6dbb56.tar.xz
blog-7c307baeb1172ca1387966338d11ede4ee6dbb56.zip
Add method "getByField"
-rw-r--r--core/namespace/Item.php17
1 files changed, 17 insertions, 0 deletions
diff --git a/core/namespace/Item.php b/core/namespace/Item.php
index d21a4d6..60f7547 100644
--- a/core/namespace/Item.php
+++ b/core/namespace/Item.php
@@ -167,5 +167,22 @@ 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;
+ }
+ }
}
?> \ No newline at end of file