diff options
author | Thomas Lange <code@nerdmind.de> | 2021-06-29 21:43:30 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-06-29 21:43:30 +0200 |
commit | 1bf75f787790f06b945c3fe214f0b18edc7c5fc9 (patch) | |
tree | 2f1902e4cbd1a7048532cc016c843f50a47fcb0f | |
parent | d87422db33faa08d443a245004a29da8fb0aaf78 (diff) | |
download | blog-1bf75f787790f06b945c3fe214f0b18edc7c5fc9.tar.gz blog-1bf75f787790f06b945c3fe214f0b18edc7c5fc9.tar.xz blog-1bf75f787790f06b945c3fe214f0b18edc7c5fc9.zip |
Add WHERE filter option to getCount method
-rw-r--r-- | core/namespace/ORM/Repository.php | 27 |
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(); } #=========================================================================== |