Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Serialize db results into JSON in wrong format #374

Closed
highroller opened this Issue · 12 comments

5 participants

@highroller

I am using Mongodb.

$results = $this->getDm()->getRepository("Bundle:User")->findAll();
$view = View::create()->setData($results);
return $this->get('fos_rest.view_handler')->handle($view);

The json becomes:
"123":{"id":"123", "abc":"fff"}, "234":{"id":"234","abc":"ggg"}
as you can see, it isn't an array, they are just a bunch of objects with id as key.

If I do this:

$results = $this->getDm()->getRepository("Bundle:User")->findAll();
foreach($results as $result)
    $json[] = $result;
$view = View::create()->setData($json);
return $this->get('fos_rest.view_handler')->handle($view);

json becomes:
[ {"id":"123", "abc":"fff"}, {"id":"234","abc":"ggg"}]

this is correct.

Is it a bug or a configuration issue? I don't want to do an unnecessary for loop if there is another way.

@lsmith77
Owner

you need took check in JMS serializer itself if there is a way to force it to use array notation for json

@highroller

I checked JMS serializer, I don't see any way to force array notation in configuration, maybe I miss something?

My problem should be very common, everybody are serializing array of results from db all the time. Why is there only one reply?

@lsmith77
Owner

there might be a solution possible. recently someone created an array serialization handler for JMS serializer. maybe you can construct something with that.

@highroller

do you mean that it is the way it is in fosrestbundle without coding custom jms serializer handler? I guess the second question is how do people handle the list of objects in client side? Their code is expecting an array.

@lsmith77
Owner
@mvrhov

We are not serializing arrays and what gets returned from doctrine is usually a (Persistent|Array)Collection or a null. Which work fine. now if you don't want to write the array serializer handler and contribute it to the JMS\serializer, then you still has the option to assign your arrays to the ArrayCollection which should work just fine.

@stof
Owner

@highroller ->findAll() is not indexing results by id. Are you overwriting it in your repository

@mvrhov please don't give wrong informations. Doctrine ORM query results are always arrays. collections are used only for relations in entities (or when using the Selectable interface)

@highroller
@stof
Owner

toArray on what ? and if you overwrite the method, you should keep the same behavior, i.e. returning a numerically indexed array. Otherwise, you are likely to cause some issues in other part of the code expecting the standard behavior.
If you want to index by id, use a custom method name instead of findAll.

and btw, array_values is your friend here.

@mvrhov

@stof: You are right the result is an array. I've forgotten that I'm making them a collections so I can pin a different metadata on them.

@lsmith77
Owner

so this can be closed?

@jrobeson

@lsmith77 : i think it can.

@lsmith77 lsmith77 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.