aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace/ORM/Repository.php
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-06-29 21:43:30 +0200
committerThomas Lange <code@nerdmind.de>2021-06-29 21:43:30 +0200
commit1bf75f787790f06b945c3fe214f0b18edc7c5fc9 (patch)
tree2f1902e4cbd1a7048532cc016c843f50a47fcb0f /core/namespace/ORM/Repository.php
parentd87422db33faa08d443a245004a29da8fb0aaf78 (diff)
downloadblog-1bf75f787790f06b945c3fe214f0b18edc7c5fc9.tar.gz
blog-1bf75f787790f06b945c3fe214f0b18edc7c5fc9.tar.xz
blog-1bf75f787790f06b945c3fe214f0b18edc7c5fc9.zip
Add WHERE filter option to getCount method
Diffstat (limited to 'core/namespace/ORM/Repository.php')
-rw-r--r--core/namespace/ORM/Repository.php27
1 files changed, 23 insertions, 4 deletions
diff --git a/core/namespace/ORM/Repository.php b/core/namespace/ORM/Repository.php
index 1eb687d..929dae3 100644
--- a/core/namespace/ORM/Repository.php
+++ b/core/namespace/ORM/Repository.php
@@ -183,11 +183,30 @@ abstract class Repository {
#===========================================================================
# Get entity count
#===========================================================================
- public function getCount(): int {
- $query = 'SELECT COUNT(id) FROM %s';
- $query = sprintf($query, static::getTableName());
+ public function getCount(array $filter = []): int {
+ $wheres = [];
+ $params = [];
+
+ if(!empty($filter)) {
+ foreach($filter as $column => $value) {
+ if($value === NULL) {
+ $wheres[] = "$column IS NULL";
+ } else {
+ $wheres[] = "$column = ?";
+ $params[] = $value;
+ }
+ }
+
+ $where = 'WHERE '.implode(' AND ', $wheres);
+ }
+
+ $query = 'SELECT COUNT(id) FROM %s %s';
+ $query = sprintf($query, static::getTableName(), $where ?? '');
+
+ $Statement = $this->Database->prepare($query);
+ $Statement->execute($params);
- return $this->Database->query($query)->fetchColumn();
+ return $Statement->fetchColumn();
}
#===========================================================================