Skip to content

Commit

Permalink
Added countBy method
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk authored Jan 4, 2023
1 parent 35ffd81 commit c787c20
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit c787c20

Please sign in to comment.