FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

Export JSON relationships on a per-case basis.

Open log0 opened this issue 14 years ago • 2 comments

I know FOSRestBundle uses JMSSerializerBundle behind the scenes, but I am also new to both. So I hope to get some directions on this. I haven't seen much directions or tutorials on that yet.

Suppose I have these associations: A has many B. B has many C. C has many D.

If I request a list of all A in JSON format, the default response data would contain all A, and all B associated with A, and all associated C with B, and all associated D with C.

My question:

  • If I want to export A with Bs only, but stops there, and does not recurse into C anymore. How would I do that on a per API basis? (I would have another API which exposes all of them)
  • If I want to export A with an extra field, which depends on B and the Cs associated with B, e.g.:

$A["cids"] = function(){ $arr = []; foreach ( $A->getBs() as $B) { $arr[] = $B->getCs(); }

return $arr; };

How would I do that in a per API basis?

Thanks!!!

log0 avatar Mar 13 '12 16:03 log0

This is indeed more a JMSSerializerBundle question. Right now I fear the only way to do this is with a @preSerialize method to manipulate the object graph before serialization to remove the things you don't want to serialize based on some property you set. Of course you could also do this before even calling the serializer. Another approach would be to simply "cast" the structure to a new object graph.

Eventually JMSSerializerBundle will get support for @group which will make this all a lot cleaner: https://github.com/schmittjoh/JMSSerializerBundle/issues/60

I am closing this ticket here. If you have further questions please open a ticket on JMSSerializerBundle.

lsmith77 avatar Mar 13 '12 16:03 lsmith77

it's not possible right now, but you might want to help with schmittjoh/JMSSerializerBundle#60

On Tue, Mar 13, 2012 at 10:53 AM, log0 < [email protected]

wrote:

I know FOSRestBundle uses JMSSerializerBundle behind the scenes, but I am also new to both. So I hope to get some directions on this. I haven't seen much directions or tutorials on that yet.

Suppose I have these associations: A has many B. B has many C. C has many D.

If I request a list of all A in JSON format, the default response data would contain all A, and all B associated with A, and all associated C with B, and all associated D with C.

My question:

  • If I want to export A with Bs only, but stops there, and does not recurse into C anymore. How would I do that on a per API basis? (I would have another API which exposes all of them)
  • If I want to export A with an extra field, which depends on B and the Cs associated with B, e.g.:

$A["cids"] = function(){ $arr = []; foreach ( $A->getBs() as $B) { $arr[] = $B->getCs(); }

return $arr; };

How would I do that in a per API basis?

Thanks!!!


Reply to this email directly or view it on GitHub: https://github.com/FriendsOfSymfony/FOSRestBundle/issues/202

schmittjoh avatar Mar 13 '12 16:03 schmittjoh