Skip to content
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

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

Open
bertrandseurot opened this issue Dec 16, 2024 · 1 comment

Comments

@bertrandseurot
Copy link

bertrandseurot commented Dec 16, 2024

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

@smnandre
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants