maker-bundle icon indicating copy to clipboard operation
maker-bundle copied to clipboard

[make:entity] collection getters should not return the internal collection ?

Open bertrandseurot opened this issue 1 year ago • 1 comments

Since make:entity generates methods responsible of the association management (add and remove), would'nt be a good idea to "protect" this behavior by returning an array of the values instead of leaking the internal collection ?

Indeed, misuse of this collection can create confusion for some. e.g. this code will not cause a database update : $foo->getBars()->remove($baz);

Here, the doctrine documentation suggests to use ->toArray() https://www.doctrine-project.org/projects/doctrine-orm/en/3.3/reference/working-with-associations.html#association-management-methods

But using ->getValues() instead could avoid well known serialization issues after a collection element removal (doctrine does not reindex the array, so a serialization to json could produce an object instead of an array) : this is what Api Platform recommends on its documentation : https://api-platform.com/docs/core/serialization/#collection-relation-using-doctrine

bertrandseurot avatar Dec 16 '24 15:12 bertrandseurot

I would not change the current behavior here.

I am not sure using arrays is a better choice than a typed collection with object oriented methods.

Also, reading the Doctrine documentation link you provided:

This will however always initialize the collection, with all the performance penalties given the size.

smnandre avatar Dec 28 '24 01:12 smnandre