Skip to content
Merged
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
34 changes: 34 additions & 0 deletions models/QuickBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,40 @@ component accessors="true" transientCache="false" {
return this;
}

/**
* Removes one or more relationships from the eager-load list. Omitting the
* argument leaves the eager-load list unchanged.
*
* @relationName A relationship name or array of relationship names to remove.
*
* @return QuickBuilder
*/
public any function without( any relationName ) {
if ( isNull( arguments.relationName ) ) {
return this;
}

var exclusions = arrayWrap( arguments.relationName );
variables._eagerLoad = variables._eagerLoad.filter( function( eagerLoad ) {
var path = isStruct( arguments.eagerLoad ) ? arguments.eagerLoad.keyArray()[ 1 ] : arguments.eagerLoad;
return !exclusions.some( function( exclusion ) {
return compareNoCase( path, exclusion ) == 0 ||
compareNoCase( left( path, len( exclusion ) + 1 ), exclusion & "." ) == 0;
} );
} );
return this;
}

/**
* Removes every configured eager load.
*
* @return QuickBuilder
*/
public any function clearEagerLoads() {
variables._eagerLoad = [];
return this;
}

/**
* Eager loads the configured relations for the retrieved entities.
* Returns the retrieved entities eager loaded with the configured
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,41 @@ component extends="tests.resources.ModuleIntegrationSpec" {
);
}
} );

it( "can disable an automatically eager loaded relationship", () => {
var posts = getInstance( "EagerLoadedPost" )
.without( "comments" )
.preventLazyLoading()
.get();

expect( posts ).toHaveLength( 4 );
expect( posts[ 1 ].isRelationshipLoaded( "comments" ) ).toBeFalse();
expect( () => posts[ 1 ].getComments() ).toThrow( type = "QuickLazyLoadingException" );
expect( variables.queries ).toHaveLength( 1, "Only the posts query should execute." );
} );

it( "does not clear eager loads when without is called without arguments", () => {
var posts = getInstance( "EagerLoadedPost" )
.without()
.preventLazyLoading()
.get();

expect( posts ).toHaveLength( 4 );
expect( posts[ 1 ].isRelationshipLoaded( "comments" ) ).toBeTrue();
expect( variables.queries ).toHaveLength( 2 );
} );

it( "can explicitly clear all eager loads", () => {
var posts = getInstance( "EagerLoadedPost" )
.clearEagerLoads()
.preventLazyLoading()
.get();

expect( posts ).toHaveLength( 4 );
expect( posts[ 1 ].isRelationshipLoaded( "comments" ) ).toBeFalse();
expect( () => posts[ 1 ].getComments() ).toThrow( type = "QuickLazyLoadingException" );
expect( variables.queries ).toHaveLength( 1, "Only the posts query should execute." );
} );
} );

describe( "multiple nested eager loads", () => {
Expand Down
Loading