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 */