e.g. if I have parent_model
and it ‘has-many’ child_model
If I access one of them e.g. iterating through objects in DB.child_model.all().toArray()
how do I access the current iteration’s parent_model and access that parent’s fields?
e.g. if I have parent_model
and it ‘has-many’ child_model
If I access one of them e.g. iterating through objects in DB.child_model.all().toArray()
how do I access the current iteration’s parent_model and access that parent’s fields?
You should be able to retrieve the parent_model
by referencing it from the child_model
as child_model.parent_model()
Example with below schema:
<model name="parent" label="Parent">
<field name="name" label="Name" type="text" />
<has-many model="child" name="children" />
<display></display>
</model>
<model name="child" label="Child">
<field name="name" label="Name" type="text" />
<belongs-to model="parent" />
<display></display>
</model>
App code:
var children = DB.child.all().toArray();
for (var i = 0; i < children.length; i++) {
var child = children[i];
var parent = child.parent();
var parentName = parent.name;
}