Count multiple aggregates¶
You can count aggregates by retrieving them and count the elements in the returned sequence like this:
$numberOfAlices = $orm
->repository(User::class)
->matching(
SearchByName::of(Name::of('alice')),
)
->sequence()
->size();
But you MUST NOT do this. This will fetch the aggregates in memory and count them in PHP, this will be extremely slow!
The right approach is:
This will run an optimized count in your storage.
But if you only need to know if there's an aggregate in the storage matching the specification, you SHOULD do:
This runs an even more optimized query against your storage.
And if you need to make sure no aggregate matches a specification:
Tip
The specification passed to any
and none
is optional, allowing you to know if there at least one aggregate or none (as the name would suggest).