Reputation: 1607
I have an Account object that contains an one-to-many relation with the newsbrief members. In Twig a want to show the quantity of members. I know that I can show the quantity with the following code:
{{ account.mailList|length }}
Only the mailList contains also inactive members. With these members their field active is false. Is there a way to filter them out? Is this way of working slowing down the application?
Upvotes: 0
Views: 2437
Reputation: 274
You can use for loop in twig:
{% set activeMailCount = 0 %}
{% for mail in account.mailList if account.mailList.active %}
{% set activeMailCount = activeMailCount + 1 %}
{% endfor %}
Upvotes: 1
Reputation: 1345
You could create a method on the account entity that returns active members
// src/AppBundle/Entity/Account.php
public function getActive()
{
$count = 0;
foreach($this->getMailList() as item) {
if (item->isActive()) { // Assuming the newsbrief members are entities
$count++ // with an $active property & isser().
}
}
return $count;
}
and call that from Twig:
{{ account.getActive() }} # or {{ account.active }}
Upvotes: 3