I have a notification icon. When user clicks on it, a dropdown appears and shows a list. I want to filter list to only show what's related to the connected user.
{% if app.user.roles[0] == "ROLE_DEVELOPPER"%}
<!-- dropdown notification message -->
<li class="dropdown">
<a title="Notifications" href="#fakelink" class="dropdown-toggle" data-toggle="dropdown">
<strong> <i class="fa fa-bell"></i></strong>
</a>
{% if notif_conges|length > 0 %}
<ul class="dropdown-menu animated half flipInX">
//this section
{% for notif_c in notif_conges %}
{% if notif_c.etat == "Acceptée" %}
<li><a>Votre demande : "{{notif_c.motif}}" Envoyée le "{{notif_c.CreatedAt|date('Y-m-d H:i:s')}}" a était traitée </a></li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
</li>
{% endif %}
You want to filter notifications. Only notifications concerning connected users have to be displayed. You have two solutions:
The (very) bad way: Add a test in view
This is a bad way because you will forward all notifications to the view
The bad way: Add a filter in your controller
This is still a bad way because you are retrieving all notifications from database.
The good way: Ask only the necessaries notification to your repository Assuming, your notifications are retrieving with Doctrine, repository can filter data to only retrieve pertinent data.
Replace
findAll()byfindBy(array $criteria)and add a criteria as first parameter: