Skip to content

Commit 87c6bbb

Browse files
authored
Merge pull request #1817 from wheels-dev/fix/nested-associations
Fix nested struct mapping with multiple belongsTo associations
2 parents 2cb6ca8 + f00063f commit 87c6bbb

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

core/src/wheels/model/serialize.cfc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,15 @@ component {
146146
}
147147
} else {
148148
// We have a hasOne or belongsTo association, so just add the object to the root object.
149+
local.joinAlias = "";
150+
if (structKeyExists(variables.wheels.class.associations[local.include], "joinAlias")) {
151+
local.joinAlias = variables.wheels.class.associations[local.include].joinAlias;
152+
}
149153
local.struct[local.include] = model(variables.wheels.class.associations[local.include].modelName).$queryRowToStruct(
150154
properties = arguments.query,
151155
row = local.i,
152-
base = false
156+
base = false,
157+
joinAlias = local.joinAlias
153158
);
154159
}
155160
}
@@ -180,20 +185,20 @@ component {
180185
public struct function $queryRowToStruct(
181186
required any properties,
182187
string name = "#variables.wheels.class.modelName#",
183-
numeric row = "1",
184-
boolean base = "true"
188+
numeric row = 1,
189+
boolean base = true,
190+
string joinAlias = ""
185191
) {
186192
local.rv = {};
187193
local.allProperties = ListToArray(ListAppend(variables.wheels.class.propertyList, variables.wheels.class.calculatedPropertyList));
188194
// a struct key is much faster than a list element
189195
local.columnStruct = $listToStruct(arguments.properties.columnList);
190-
local.iEnd = ArrayLen(local.allProperties);
191-
for (local.i = 1; local.i <= local.iEnd; local.i++) {
196+
local.prefix = Len(arguments.joinAlias)? arguments.joinAlias: arguments.name;
197+
for (local.item in local.allProperties) {
192198
// Wrap in try/catch because coldfusion has a problem with empty strings in queries for bit types.
193199
try {
194-
local.item = local.allProperties[local.i];
195-
if (!arguments.base && StructKeyExists(local.columnStruct, arguments.name & local.item)) {
196-
local.rv[local.item] = arguments.properties[arguments.name & local.item][arguments.row];
200+
if (!arguments.base && StructKeyExists(local.columnStruct, local.prefix & local.item)) {
201+
local.rv[local.item] = arguments.properties[local.prefix & local.item][arguments.row];
197202
} else if (StructKeyExists(local.columnStruct, local.item)) {
198203
local.rv[local.item] = arguments.properties[local.item][arguments.row];
199204
}

0 commit comments

Comments
 (0)