From 553d4f32bc9107f58fedc3b8ddb83f1a34379812 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Sat, 22 Aug 2026 07:11:29 -0600 Subject: [PATCH] fix: qualify aliased selected attributes Closes #40 --- models/BaseEntity.cfc | 10 ++++++++++ tests/specs/integration/BaseEntity/AsQuerySpec.cfc | 12 ++++++++++++ 2 files changed, 22 insertions(+) diff --git a/models/BaseEntity.cfc b/models/BaseEntity.cfc index a3f7a943..5e4357ca 100644 --- a/models/BaseEntity.cfc +++ b/models/BaseEntity.cfc @@ -381,6 +381,16 @@ component accessors="true" { string tableName = this.tableName(), boolean useParentLookup = true ) { + if ( reFindNoCase( "\s+AS\s+", arguments.column ) ) { + var source = trim( reReplaceNoCase( arguments.column, "\s+AS\s+.*$", "" ) ); + var alias = trim( reReplaceNoCase( arguments.column, "^.*?\s+AS\s+", "" ) ); + return qualifyColumn( + column = source, + tableName = arguments.tableName, + useParentLookup = arguments.useParentLookup + ) & " AS " & alias; + } + if ( findNoCase( ".", arguments.column ) != 0 || !hasAttribute( arguments.column ) || diff --git a/tests/specs/integration/BaseEntity/AsQuerySpec.cfc b/tests/specs/integration/BaseEntity/AsQuerySpec.cfc index d2dc845a..abc77c93 100644 --- a/tests/specs/integration/BaseEntity/AsQuerySpec.cfc +++ b/tests/specs/integration/BaseEntity/AsQuerySpec.cfc @@ -77,6 +77,18 @@ component extends="tests.resources.ModuleIntegrationSpec" { expect( results[ 2 ][ "latestPostId" ] ).toBe( "" ); } ); + it( "can select an aliased entity attribute when returning query data", function() { + var result = getInstance( "User" ) + .select( [ "firstName AS firstName" ] ) + .where( "id", 1 ) + .asQuery( withAliases = false ) + .first(); + + expect( result ).toBeStruct(); + expect( result ).toHaveKey( "firstName" ); + expect( result.firstName ).toBe( "Eric" ); + } ); + it( "can do eager loading", function() { var results = getInstance( "Post" ) .newQuery()