JSON Object representation using PHP

I have an object of Class A that has Advanced many-to-many relations have many objects of class B. I’d like to get a JSON representation of Object A and all children Objects, and their properties, etc. What is the best way to accomplish this?

I already have a reference to the Object of Class A as $obj in PHP. Tried the documentation,

thank you!

1 Like

So I created custom models for that and implemented assemblers for those (Assembler Pattern). Then I created a symfony normalizer for each of my dataobjects where I inject the assembler. I let the assembler run and then normalize the object. That’s enabled the symfony serializer to serialize the given dataobject into whatever I want.

Does it help you? I’m sure there are other approaches out there, but that was mine.

Very interesting… but for a PHP novice like me this sounds like an ambitious undertaking and I wouldn’t know the impact of this approach on how we are using Pimcore for our processes… having said that, i’d love it if you could point me to the resources as I wrap my brain around this approach:

  1. Assembler Pattern for Pimcore objects created using the front-end
  2. Injecting the pattern on the object

thanks!

I found that article, it’s close to what I’m doing, maybe it helps you?

Basically it’s just about creating a object structure close to your json. As you found out it’s pretty hard to serialize the data object into json or set it from json. The assembler class basically owns the know how about converting the data object to dto and dto back to the data object - if you need that.

Here is more info about custom normalizer!

Implementing Fractal has worked for me. You can create transformation classes for both the parent and its children. Adding a twig filter to call the transformations helped a lot with passing objects to our vuejs components.

https://fractal.thephpleague.com/