From 49e278ad081e90631db399fd7c1b66b46dfd5633 Mon Sep 17 00:00:00 2001 From: zainforbjs Date: Wed, 17 Dec 2025 19:26:56 +0500 Subject: [PATCH 1/2] Fix nested struct mapping with multiple belongsTo associations Updated $queryRowToStruct to accept joinAlias and map columns correctly per association. - Modified serializeQueryToArray to pass joinAlias when available. - Ensures each nested association now receives only its relevant columns. --- core/src/wheels/model/serialize.cfc | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/core/src/wheels/model/serialize.cfc b/core/src/wheels/model/serialize.cfc index 174b54f2c..c56b40ef4 100644 --- a/core/src/wheels/model/serialize.cfc +++ b/core/src/wheels/model/serialize.cfc @@ -146,10 +146,15 @@ component { } } else { // We have a hasOne or belongsTo association, so just add the object to the root object. + local.joinAlias = ""; + if (structKeyExists(variables.wheels.class.associations[local.include], "joinAlias")) { + local.joinAlias = variables.wheels.class.associations[local.include].joinAlias; + } local.struct[local.include] = model(variables.wheels.class.associations[local.include].modelName).$queryRowToStruct( properties = arguments.query, row = local.i, - base = false + base = false, + joinAlias = variables.wheels.class.associations[local.include].joinAlias ); } } @@ -180,20 +185,20 @@ component { public struct function $queryRowToStruct( required any properties, string name = "#variables.wheels.class.modelName#", - numeric row = "1", - boolean base = "true" + numeric row = 1, + boolean base = true, + string joinAlias = "" ) { local.rv = {}; local.allProperties = ListToArray(ListAppend(variables.wheels.class.propertyList, variables.wheels.class.calculatedPropertyList)); // a struct key is much faster than a list element local.columnStruct = $listToStruct(arguments.properties.columnList); - local.iEnd = ArrayLen(local.allProperties); - for (local.i = 1; local.i <= local.iEnd; local.i++) { + local.prefix = Len(arguments.joinAlias)? arguments.joinAlias: arguments.name; + for (local.item in local.allProperties) { // Wrap in try/catch because coldfusion has a problem with empty strings in queries for bit types. try { - local.item = local.allProperties[local.i]; - if (!arguments.base && StructKeyExists(local.columnStruct, arguments.name & local.item)) { - local.rv[local.item] = arguments.properties[arguments.name & local.item][arguments.row]; + if (!arguments.base && StructKeyExists(local.columnStruct, local.prefix & local.item)) { + local.rv[local.item] = arguments.properties[local.prefix & local.item][arguments.row]; } else if (StructKeyExists(local.columnStruct, local.item)) { local.rv[local.item] = arguments.properties[local.item][arguments.row]; } From f00063fe91a1df7b4248d53be1d6256d9408da6d Mon Sep 17 00:00:00 2001 From: zainforbjs Date: Thu, 18 Dec 2025 11:42:23 +0500 Subject: [PATCH 2/2] Fix typo --- core/src/wheels/model/serialize.cfc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/wheels/model/serialize.cfc b/core/src/wheels/model/serialize.cfc index c56b40ef4..1a1649d9d 100644 --- a/core/src/wheels/model/serialize.cfc +++ b/core/src/wheels/model/serialize.cfc @@ -154,7 +154,7 @@ component { properties = arguments.query, row = local.i, base = false, - joinAlias = variables.wheels.class.associations[local.include].joinAlias + joinAlias = local.joinAlias ); } }