Skip to content

[Form] collection type is broken #3539

Closed
@mvrhov

Description

@mvrhov

This one is a bit hard to explain, but I'll try my best.

PR#3239 brought proper handling of collections, but it doesn't work as expected. I've traced this only for blank collection, but this is probably the same or similar when you go and edit existing one.
My collection object is having duplicates. And if I take a look at the memory addresses of those duplicates with the debugger, I can see that the same objects are added twice as they reside on the same address.

It seems, that the data is bind to the collection twice, first in the form's bind() method and then once again in MergeCollectionListener.

Looking at the $originalData when onBindNormData is called in
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/EventListener/MergeCollectionListener.php#L142
I can see, that $originalData already contains the whole collection that came in through the request.

Later down the code calls add* method and adds the same objects.
Now the problem is not visible if all you do with the data is just call $em->persist($form->getData()
as doctrine will silently handle this, but if you need to traverse the collection and do some calculations
on it before persisting, then you have the problem as you'll do the whatever you want to do twice.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions