Assume I have a User
and a Message
entity in my Symfony2 app, with a oneToMany
relation between both entities, so a User can have multiple messages, and I want to display the number of unread messages in my main navigation like this
- Home
- Whatever
- Messages (6)
- My Profile
where (6)
is the number of unread messages. In order to fetch this amount of unread messages per user Controller independently, I could think of defining a User
entity method:
public function getUnreadMessagesCount() {
$unreadMessagesCount = 0;
foreach($this->messages as $message) {
if($message->isUnread()) {
$unreadMessagesCount++;
}
}
}
and call it in my menu.html.twig
{% set umc = app.user.getUnreadMessagesCount() %}
{% if umc > 0 %}
({{ umc }})
{% endif %}
But for performance reasons, I would want to avoid looping through the entire set of messages just to check whether it is unread or not. So I would prefer to fetch it via a DQL call (and place this fetch in the UserRepository
class)
$query = $em->createQuery("
SELECT COUNT(m.id) AS umc
FROM AcmeBundle:User u
JOIN AcmeBundle:Message m
WITH u.id = m.user_id
WHERE m.unread = true
AND u.id = :uid");
But afaik it's bad practice to use repository methods or to use the entitiy manager (or Doctrine) in entity classes.
I thought about fetching the value in a service and inject this into my entity. But it also is not recommended to inject services into entites.
So what is a legit way to fetch this value and output it in all templates (may also be without entity methods, e.g. by injecting the value into all Controllers)?