From c787c20e45607ccaf4a5afe73935c97b4b60c577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Proch=C3=A1zka?= Date: Wed, 4 Jan 2023 15:11:11 +0100 Subject: [PATCH] Added countBy method --- src/AbstractRepository.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/AbstractRepository.php b/src/AbstractRepository.php index 862756a..732a84f 100644 --- a/src/AbstractRepository.php +++ b/src/AbstractRepository.php @@ -109,6 +109,19 @@ public function findById(int $id): ?object } + public function countBy(callable $where): int|float + { + /** @see Hardcoded indexBy might cause issues with entities without $id */ + $qb = $this->createQueryBuilder('e', 'e.id', function($qb) use ($where) { + $qb->select('count(e.id)'); + return ($where($qb) ?: $qb); + }); + + return $qb->getQuery()->setMaxResults(1) + ->getSingleScalarResult(); + } + + /** * @internal */