-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Closed as not planned
Description
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:
#[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:
$subordinates = $boss->getSubordinates()->toArray();
return $this->render('index.html.twig',
[
'subordinates' => $subordinates,
]);
Why is that?
Is this really a bug?
Additional Context
No response