Using array_splice for removing collection items instead of unset

17 views
Skip to first unread message

naitsirch

unread,
Sep 30, 2019, 6:03:14 AM9/30/19
to doctrine-user
Hi,

I would like to ask why `unset` is used when collection items are removed (e.g. https://github.com/doctrine/collections/blob/master/lib/Doctrine/Common/Collections/ArrayCollection.php#L150). Is it required for other functions in Doctrine to work?

The issue with `unset` is, that it leaves a "hole" in the array. This is no problem when you work with PHP. But when you are working with serialization (JSON) and Javascript in the frontend. This is a pain, because an PHP array that is missing one element, will be encoded as a JSON object instead of an array. And in the frontend where you work with Javascript, things like collection.map will fail because an object does not have `map`.


Marco Pivetta

unread,
Sep 30, 2019, 6:10:31 AM9/30/19
to doctri...@googlegroups.com
Heya,

Yes, this is correct: the keys should remain stable after loading. Collections also don't guarantee the keys to be integer-indexed or string-indexed, so no assumptions are being made there: this is a design decision that cannot be changed at this point without breaking BC for others.



--
You received this message because you are subscribed to the Google Groups "doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to doctrine-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/doctrine-user/49879c7c-17f7-43ff-8f71-c5ed67af2c48%40googlegroups.com.

naitsirch

unread,
Sep 30, 2019, 6:14:15 AM9/30/19
to doctrine-user
I see. Thanks for your reply.


Am Montag, 30. September 2019 12:10:31 UTC+2 schrieb Marco Pivetta:
Heya,

Yes, this is correct: the keys should remain stable after loading. Collections also don't guarantee the keys to be integer-indexed or string-indexed, so no assumptions are being made there: this is a design decision that cannot be changed at this point without breaking BC for others.



On Mon, Sep 30, 2019 at 12:03 PM naitsirch <nait...@e.mail.de> wrote:
Hi,

I would like to ask why `unset` is used when collection items are removed (e.g. https://github.com/doctrine/collections/blob/master/lib/Doctrine/Common/Collections/ArrayCollection.php#L150). Is it required for other functions in Doctrine to work?

The issue with `unset` is, that it leaves a "hole" in the array. This is no problem when you work with PHP. But when you are working with serialization (JSON) and Javascript in the frontend. This is a pain, because an PHP array that is missing one element, will be encoded as a JSON object instead of an array. And in the frontend where you work with Javascript, things like collection.map will fail because an object does not have `map`.


--
You received this message because you are subscribed to the Google Groups "doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to doctri...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages