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 · 7 comments
Closed

Entity field saved as json_object instead of json_array #23653

OkieRazorback opened this issue Jul 24, 2017 · 7 comments
Labels

Comments

@OkieRazorback
Copy link

@OkieRazorback OkieRazorback commented Jul 24, 2017

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

This comment has been minimized.

Copy link
Member

@javiereguiluz javiereguiluz commented Jul 25, 2017

@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

This comment has been minimized.

Copy link
Contributor

@jvasseur jvasseur commented Jul 25, 2017

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

This comment has been minimized.

Copy link
Author

@OkieRazorback OkieRazorback commented Jul 25, 2017

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

This comment has been minimized.

Copy link
Member

@javiereguiluz javiereguiluz commented Oct 25, 2017

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

@HeahDude

This comment has been minimized.

Copy link
Member

@HeahDude HeahDude commented Oct 25, 2017

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

This comment has been minimized.

Copy link
Member

@HeahDude HeahDude commented Oct 25, 2017

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

This comment has been minimized.

Copy link
Member

@xabbuh xabbuh commented Feb 21, 2018

closing because of the reasons given by @HeahDude

@xabbuh xabbuh closed this Feb 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.