Skip to content

[TwigBundle] Children Collection contains parent #60311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DavidPetrasek opened this issue May 1, 2025 · 4 comments
Closed

[TwigBundle] Children Collection contains parent #60311

DavidPetrasek opened this issue May 1, 2025 · 4 comments

Comments

@DavidPetrasek
Copy link

DavidPetrasek commented May 1, 2025

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

@xabbuh
Copy link
Member

xabbuh commented May 1, 2025

Can you create a small example application that allows to reproduce your issue?

@DavidPetrasek
Copy link
Author

Most likely later when I have the time.

And I also found that this works:

$subordinates = $boss->getSubordinates()->toArray();

return $this->render('index.html.twig', 
        [
            'subordinates' =>  $subordinates,
        ]);

... but this doesn't:

return $this->render('index.html.twig', 
        [
            'subordinates' =>  $boss->getSubordinates()->toArray(),
        ]);

@xabbuh
Copy link
Member

xabbuh commented May 5, 2025

Do you have some more infos for us that allow us to reproduce your issue?

@xabbuh
Copy link
Member

xabbuh commented May 7, 2025

I am going to close here for now due to the lack of feedback. Please let us know when you have more information and we can consider to reopen.

@xabbuh xabbuh closed this as not planned Won't fix, can't repro, duplicate, stale May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants