symfony-collection icon indicating copy to clipboard operation
symfony-collection copied to clipboard

Issues with indexed collection

Open enumag opened this issue 9 years ago • 6 comments

For certain reasons I decided to use indexed collection:

    /**
     * @OneToMany(targetEntity = "ContactPerson", mappedBy = "company", indexBy = "id", cascade = { "persist", "remove" }, orphanRemoval = true)
     * @Valid()
     * @var Collection
     */
    private $contactPersons;

When using this with the collection type in symfony/form and this plugin there are certain issues.

First error mapping doesn't work for newly added items. Let's say I have to items in the collection with indexes 1 and 2. Then in the form I add a third item. This script will use index 0 for it because it's not occupied. Symfony however will add it under index 3. That's a PHP thing - when array contains int keys and you add an item it generates new key as current max + 1, not the lowest unused. Of course validation errors won't be mapped correctly because 0 != 3.

Now this can be fixed using some different logic to generate the keys in the doAdd function.

What I'm not sure about though is how to make sure that the keys of deleted items won't be reused by newly added items. I need that because I want the database to perform DELETE + INSERT, not UPDATE. Any ideas?

enumag avatar Feb 02 '16 13:02 enumag

Or actually I do know how to fix it but I'd have to pass my own index generator to this script.

enumag avatar Feb 02 '16 13:02 enumag

Hello,

I think I didn't try this so there are good chances there are bugs ^^.

Will look at this as soon as possible; but I'm really busy those days (until middle of march I think) so don't worry if I take long to answer.

If you know how to fix it, don't hesitate to add a PR if you can.

Thanks for the report!

Alain

ninsuo avatar Feb 02 '16 17:02 ninsuo

Turns out its not as easy as I thought. When I use a indexed collection I don't really care about the order of the items. With that in mind it doesn't make sense to have a [+] button after each item - there should be just one at the end. Basically rescue-add should always be visible and normal add buttons should not be used at all. Also duplication would need to be changed to add the item to the end.

I might try to prepare a PR but I'm quite confused about some things in the code so don't expect much.

enumag avatar Feb 02 '16 17:02 enumag

No problem. I should add a new option to allow using only "rescue" button. Will see if that's hard or not and will fire a PR soon if that's not.

ninsuo avatar Feb 02 '16 18:02 ninsuo

@ninsuo Oh wait, there already is an option like that. http://symfony-collection.fuz.org/options/addButtonAtTheBottom

It doesn't work for me for some reason though. I'll look into it.

enumag avatar Feb 02 '16 18:02 enumag

@ninsuo See #21. That doesn't fix the error mapping issue of course.

enumag avatar Feb 02 '16 20:02 enumag