diff --git a/models/Relationships/BaseRelationship.cfc b/models/Relationships/BaseRelationship.cfc index 6850674..2653549 100644 --- a/models/Relationships/BaseRelationship.cfc +++ b/models/Relationships/BaseRelationship.cfc @@ -359,22 +359,24 @@ component accessors="true" implements="IRelationship" { required array keys, required any baseEntity ) { - return unique( - arguments.entities.reduce( function( acc, entity ) { - var keyValues = []; - for ( var key in keys ) { - var value = structKeyExists( entity, "isQuickEntity" ) ? entity.retrieveAttribute( key ) : entity[ key ]; - if ( entityIsNullValue( baseEntity, key, value ) ) { - return acc; - } - keyValues.append( value ); + var uniqueKeys = arguments.entities.reduce( function( acc, entity ) { + var keyValues = []; + for ( var key in keys ) { + var value = structKeyExists( entity, "isQuickEntity" ) ? entity.retrieveAttribute( key ) : entity[ key ]; + if ( entityIsNullValue( baseEntity, key, value ) ) { + return acc; } - acc.append( keyValues.toList() ); - return acc; - }, [] ) - ).map( function( key ) { - return key.listToArray(); - } ); + keyValues.append( value ); + } + + acc[ serializeJSON( keyValues ) ] = keyValues; + return acc; + }, {} ); + + return uniqueKeys.reduce( function( acc, _, keyValues ) { + acc.append( keyValues ); + return acc; + }, [] ); } /** diff --git a/models/Relationships/BelongsTo.cfc b/models/Relationships/BelongsTo.cfc index d902118..ea03e2f 100644 --- a/models/Relationships/BelongsTo.cfc +++ b/models/Relationships/BelongsTo.cfc @@ -164,7 +164,7 @@ component * @return [any] */ public array function getEagerEntityKeys( required array entities, required any baseEntity ) { - return arguments.entities + var uniqueKeys = arguments.entities .reduce( function( keys, entity ) { var values = variables.foreignKeys .map( function( foreignKey ) { @@ -197,15 +197,16 @@ component } ); if ( values.len() == variables.foreignKeys.len() ) { - arguments.keys[ values.toList() ] = {}; + arguments.keys[ serializeJSON( values ) ] = values; } return arguments.keys; - }, {} ) - .keyArray() - .map( function( key ) { - return key.listToArray(); - } ); + }, {} ); + + return uniqueKeys.reduce( function( acc, _, values ) { + acc.append( values ); + return acc; + }, [] ); } /** diff --git a/tests/specs/integration/BaseEntity/Relationships/EagerLoadingSpec.cfc b/tests/specs/integration/BaseEntity/Relationships/EagerLoadingSpec.cfc index f902478..dc28cc6 100644 --- a/tests/specs/integration/BaseEntity/Relationships/EagerLoadingSpec.cfc +++ b/tests/specs/integration/BaseEntity/Relationships/EagerLoadingSpec.cfc @@ -34,6 +34,19 @@ component extends="tests.resources.ModuleIntegrationSpec" { } } ); + it( "preserves numeric binding types when eager loading a belongs to relationship", function() { + getInstance( "Post" ).with( "author" ).get(); + + expect( variables.queries ).toHaveLength( 2 ); + var bindingTypes = extractBindingTypes( variables.queries[ 2 ] ); + + expect( bindingTypes ).notToBeEmpty(); + for ( var bindingType in bindingTypes ) { + expect( bindingType ).notToInclude( "varchar" ); + expect( bindingType ).toInclude( "integer" ); + } + } ); + it( "can eager load a belongs to relationship using a composite key", function() { var compositeChildren = getInstance( "CompositeChild" ).with( "parent" ).get(); expect( compositeChildren ).toBeArray(); @@ -243,6 +256,19 @@ component extends="tests.resources.ModuleIntegrationSpec" { expect( variables.queries ).toHaveLength( 2, "Only two queries should have been executed." ); } ); + it( "preserves numeric binding types when eager loading a belongs to many relationship", function() { + getInstance( "Post" ).with( "tags" ).get(); + + expect( variables.queries ).toHaveLength( 2 ); + var bindingTypes = extractBindingTypes( variables.queries[ 2 ] ); + + expect( bindingTypes ).notToBeEmpty(); + for ( var bindingType in bindingTypes ) { + expect( bindingType ).notToInclude( "varchar" ); + expect( bindingType ).toInclude( "integer" ); + } + } ); + it( "can eager load a has many through relationship", function() { var countries = getInstance( "Country" ).with( "posts" ).get(); expect( countries ).toBeArray(); @@ -747,4 +773,14 @@ component extends="tests.resources.ModuleIntegrationSpec" { arrayAppend( variables.queries, interceptData ); } + private array function extractBindingTypes( required struct queryLogEntry ) { + return arguments.queryLogEntry.bindings + .filter( function( binding ) { + return isStruct( binding ) && ( binding.keyExists( "cfsqltype" ) || binding.keyExists( "sqltype" ) ); + } ) + .map( function( binding ) { + return lCase( binding.cfsqltype ?: binding.sqltype ); + } ); + } + }