There are other questions on this but they don't cover my use case, I think.
I have a User, a User has Projects. A Project can be published or not.
Within Twig (in several places, in several templates) I wish to display the number of published Projects a User has.
This information is not stored, directly, within the Entity of the User. I could write a function to do this within the User Entity, but that would involve iterating over the Projects and checking each one's published status. This seems slow.
The other way is to write a query within the User Repo and get a count of all the published Projects. But that would mean ensuring that query from the Repo is executed in each Controller and then assigned to each Twig view. This feels suboptimal also as I need to count in lots of places and so would be repeating this Repo query in lots of places. Possibly in every Controller.
How can I therefore get this count from within Twig, from the User Entity or from the Repo?