You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When iterating over the children collection (subordinates) in a Twig template, it also renders the parent/owner of the collection (the boss). It should only render the subordinates.
How to reproduce
Controller:
#[Route('/index', name: 'index', methods: ['GET'])]
public function index (): Response
{
...
$subordinates = $boss->getSubordinates();
return $this->render('index.html.twig',
[
'subordinates' => $subordinates,
]);
}
Excerpt from CompanyEmployee Entity:
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'subordinates')]
private ?self $boss = null;
#[ORM\OneToMany(mappedBy: 'boss', targetEntity: self::class, orphanRemoval: false, cascade: ['persist'])]
private Collection $subordinates;
...
public function __construct()
{
$this->subordinates = new ArrayCollection();
}
...
/**
* @return Collection<int, self>
*/
public function getSubordinates(): Collection
{
return $this->subordinates;
}
index.html.twig:
{% for subordinate in subordinates %}
<div>{{ subordinate.name }}</div>
{% endfor %}
Possible Solution
What works is to convert the Collection to array in the Controller like this:
Symfony version(s) affected
7.2.5
Description
When iterating over the children collection (subordinates) in a Twig template, it also renders the parent/owner of the collection (the boss). It should only render the subordinates.
How to reproduce
Controller:
Excerpt from
CompanyEmployee
Entity:index.html.twig:
Possible Solution
What works is to convert the Collection to array in the Controller like this:
Why is that?
Is this really a bug?
Additional Context
No response
The text was updated successfully, but these errors were encountered: