Skip to content

EntityRepository#count() should support Criteria as parameter #12261

@jurchiks

Description

@jurchiks

Feature Request

What

https://github.com/doctrine/orm/blob/3.5.x/src/EntityRepository.php#L138
EntityRepository#count() calls EntityPersister#count(), which defines itself as public function count(array|Criteria $criteria = []): int. This methos has two implementations, AbstractEntityPersister#count() and BasicEntityPersister#count(), both of which have the same method signature.
However, EntityRepository#count() is defined as public function count(array $criteria = []): int, completely discarding the Criteria option.

Please add Criteria to this parameter to make it match the interface and improve UX.

Why

Because it makes sense.
Instead of having to write a custom EntityRepository method, users could write something like this:

$newUsersThisMonth = $em->getRepository(User::class)->count(
    Criteria::expr()->gte('createdAt', new DateTime('midnight first day of this month'))
);

How

-public function count(array $criteria = []): int
+public function count(array|Criteria $criteria = []): int

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions