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

Entity field saved as json_object instead of json_array #23653

Closed
OkieRazorback opened this issue Jul 24, 2017 · 8 comments
Closed

Entity field saved as json_object instead of json_array #23653

OkieRazorback opened this issue Jul 24, 2017 · 8 comments

Comments

@OkieRazorback
Copy link

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.3.5

When persisting an entity that has had an item removed from the array the data is save as a json_object instead of json_array.

Expected

["item1","item2","item3"]

Actual

{"0":"item1","1":"item2","2","item3"}

This seems to only happen when removing an item from the array and I believe it has to do with the way php handles array indexes. From my debugging it appears that when the array indexes are not sequential (ie 0,2,3) it is seen as an associative array which causes the data to be persisted as a json_object instead of json_array. The problem does not occur as long as there are no array items or the indexes are sequential.

@javiereguiluz
Copy link
Member

@OkieRazorback thanks for reporting this, but I'm afraid we'd need more information. The reason is that Symfony doesn't persist entities, it's Doctrine who does that. So, where do you think this issue happens? When submitting some info in a form, when handling some data in a request or when executing the Doctrine logic? Thanks!

@jvasseur
Copy link
Contributor

This logic comme from the php json_encode function used by doctrine, so there is nothing we can do here.

You should use array_values in your entity code if you want to ensure that data is saved as a json array.

@OkieRazorback
Copy link
Author

The reason I think this is happening in Symfony is if I output the request data the array is ordered properly (1,2,3) but after it runs through $form->handleRequest() it is ordered (0,2,3).

I was able to work around the issue by using array_values in a data_transformer as @jvasseur suggested.

@javiereguiluz
Copy link
Member

@HeahDude would you consider this a bug of $form->handleRequest() or is this the expected behavior? Thanks!

@HeahDude
Copy link
Contributor

It may come from the ResizeFormListener if this is about a collection field. But we don't have enough information to be sure of what's actually happening.

@OkieRazorback could you please create a reproducer for this bug? Thanks!

@HeahDude
Copy link
Contributor

Actually it's not a bug, it is expected that on removal the listener keeps the index of each element.

So using a transformer as proposed by @jvasseur is half a solution because in your case it has to be a model transformer which does not reapply the changes on the view data if the view is generated after submission (invalid or without redirection).

So it must happen in a SUBMIT event after the ResizeFormListener.

Not sure this is worth to add such behavior in the core with a proper option like reset_indexes or if a note in doc would be enough.

@xabbuh
Copy link
Member

xabbuh commented Feb 21, 2018

closing because of the reasons given by @HeahDude

@xabbuh xabbuh closed this as completed Feb 21, 2018
@nicolasmoreira
Copy link

nicolasmoreira commented May 19, 2020

Same issue here, I was able to solve the issue adding array_values on entity: $this->propertyWithIssue = array_values($propertyWithIssue);

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

No branches or pull requests

7 participants