Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions models/Relationships/BaseRelationship.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}, {} );
Comment on lines +362 to +374

return uniqueKeys.reduce( function( acc, _, keyValues ) {
acc.append( keyValues );
return acc;
}, [] );
}

/**
Expand Down
15 changes: 8 additions & 7 deletions models/Relationships/BelongsTo.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}, [] );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
} );
}

}
Loading