Skip to content

Commit 399bb93

Browse files
committed
feat(EagerLoading): Give entities a way to automatically eager load certain relationships
Using the `variables._with` property on an entity, you can specify relationships to always eager load. Be careful with this as it is easy to over fetch data this way.
1 parent 5939192 commit 399bb93

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

models/BaseEntity.cfc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ component accessors="true" {
6565
*/
6666
property name="_table" persistent="false";
6767

68+
/**
69+
* An array of relationships to automatically eager load.
70+
* Use with caution as it is easy to over fetch using this.
71+
*/
72+
property name="_with" persistent="false";
73+
6874
/**
6975
* A struct of query options for query executions.
7076
*/
@@ -245,7 +251,7 @@ component accessors="true" {
245251
param variables._data = {};
246252
param variables._relationshipsData = {};
247253
param variables._relationshipsLoaded = {};
248-
param variables._eagerLoad = [];
254+
param variables._with = [];
249255
variables._withoutRelationshipConstraints = createObject( "java", "java.util.HashSet" ).init();
250256
variables._applyingGlobalScopes = false;
251257
variables._globalScopesApplied = false;
@@ -1024,7 +1030,6 @@ component accessors="true" {
10241030
}
10251031
variables._relationshipsData = {};
10261032
variables._relationshipsLoaded = {};
1027-
variables._eagerLoad = [];
10281033
variables._loaded = arguments.toNew ? false : variables._loaded;
10291034

10301035
return this;
@@ -2104,7 +2109,7 @@ component accessors="true" {
21042109
return variables._wirebox.getInstance(
21052110
name = "PolymorphicBelongsTo@quick",
21062111
initArguments = {
2107-
"related" : this.set_EagerLoad( [] ).resetQuery(),
2112+
"related" : this.resetQuery(),
21082113
"relationName" : relationName,
21092114
"relationMethodName" : arguments.relationMethodName,
21102115
"parent" : this,
@@ -2241,7 +2246,8 @@ component accessors="true" {
22412246
.set_lazyLoadingViolationCallback( variables._lazyLoadingViolationCallback )
22422247
.mergeDefaultOptions( variables._queryOptions )
22432248
.from( tableName() )
2244-
.addSelect( retrieveQualifiedColumns() );
2249+
.addSelect( retrieveQualifiedColumns() )
2250+
.with( variables._with );
22452251

22462252
if ( variables._meta.originalMetadata.keyExists( "grammar" ) ) {
22472253
newBuilder.setGrammar( variables._wirebox.getInstance( variables._meta.originalMetadata.grammar ) );

models/Relationships/PolymorphicBelongsTo.cfc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ component extends="quick.models.Relationships.BelongsTo" accessors="true" {
169169
}
170170

171171
return arguments.instance
172-
.with( variables.related.get_eagerLoad() )
173172
.when( arguments.asQuery, function( qb ) {
174173
qb.asQuery( withAliases );
175174
} )
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
component extends="Post" accessors="true" {
2+
3+
variables._with = [ "comments" ];
4+
5+
}

tests/specs/integration/BaseEntity/Relationships/EagerLoadingSpec.cfc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,25 @@ component extends="tests.resources.ModuleIntegrationSpec" {
590590
);
591591
} );
592592
} );
593+
594+
describe( "automatic eager loading", () => {
595+
it( "will automatically eager load specified relationships", () => {
596+
var posts = getInstance( "EagerLoadedPost" ).preventLazyLoading().get();
597+
expect( posts ).toBeArray();
598+
expect( posts ).toHaveLength( 4, "4 posts should have been loaded" );
599+
for ( var post in posts ) {
600+
expect( () => {
601+
post.getComments();
602+
} ).notToThrow( type = "QuickLazyLoadingException" );
603+
}
604+
if ( arrayLen( variables.queries ) != 2 ) {
605+
expect( variables.queries ).toHaveLength(
606+
2,
607+
"Only two queries should have been executed. #arrayLen( variables.queries )# were instead."
608+
);
609+
}
610+
} );
611+
} );
593612
} );
594613
}
595614

0 commit comments

Comments
 (0)