I have an object with one to many relationship:
property name=“widget” fieldtype=“one-to-many” cfc=“Widget” type=“array” cascade=“delete-orphan” inverse=“true” lazy=“true”;
If I try to get object and output as json:
var myObject= objectService.get(rc.id);
I get a json with my object and also children in a json format. However with the rerlationship set to lazy=true I was expecting only the object without children.
How renderdata works? it traverse all object property? can I exclude children?
I’m on Railo and I know there are some difference with ACF on how it handle the lazy loading relationship.
I believe it basically just calls the native serializeJSON() function, so whatever that function does is what you get.
ColdBox Platform Evangelist
Ortus Solutions, Corp
ColdBox Platform: http://www.coldbox.org
I would strongly encourage you not to try to do this with ORM entities. In my personal experience, not only is the serialization a bit fiddly (at least in ACF), but you could open yourself up to a massive pile of JSON, depending on how complex your relationships are on a given entity.
You could develop a strategy that traverses the properties of the given object and build out a desired, simpler object that can be easily and consistently serialized. For example, in ContentBox each entity (or most that need it, at least) has a getMemento() method that takes the current object and prepares a struct of the properties (and relational properties, as needed) that is returned and can be serialized (or used for other purposes).
With such a strategy, you not only have very granular control over children (and children of children, etc), but you can even create rules for different scenarios where your serialization can be different.
I have created a similar function to getMemento, to have a struct with my property and some check to exclude children.
Many thanks guys!!!
Cool, glad it was helpful!