Skip to content

Commit

Permalink
fix(Relationship): Required the relationship name when disabling cons…
Browse files Browse the repository at this point in the history
…traints to match on

This helps us ensure we only skip loading constraints for the correct relationship.

BREAKING CHANGE: Any usage of `withoutRelationshipConstraints` needs to
pass the relationship function name to the callback as the first argument.
  • Loading branch information
elpete committed Mar 26, 2024
1 parent 0313951 commit dff52d6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
33 changes: 17 additions & 16 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1266,8 +1266,8 @@ component accessors="true" {
*
* @callback The callback to run without any automatic relationship constraints.
*/
public any function withoutRelationshipConstraints( required any callback ) {
variables._withoutRelationshipConstraints = true;
public any function withoutRelationshipConstraints( required string relationshipName, required any callback ) {
variables._withoutRelationshipConstraints = arguments.relationshipName;
try {
return arguments.callback();
} finally {
Expand Down Expand Up @@ -1454,7 +1454,7 @@ component accessors="true" {
"parent" : this,
"foreignKeys" : arguments.foreignKey,
"localKeys" : arguments.localKey,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand Down Expand Up @@ -1509,7 +1509,7 @@ component accessors="true" {
"parent" : this,
"foreignKeys" : arguments.foreignKey,
"localKeys" : arguments.localKey,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand Down Expand Up @@ -1564,7 +1564,7 @@ component accessors="true" {
"parent" : this,
"foreignKeys" : arguments.foreignKey,
"localKeys" : arguments.localKey,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand Down Expand Up @@ -1655,7 +1655,7 @@ component accessors="true" {
"relatedPivotKeys" : arguments.relatedPivotKey,
"parentKeys" : arguments.parentKey,
"relatedKeys" : arguments.relatedKey,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand Down Expand Up @@ -1704,7 +1704,7 @@ component accessors="true" {
private HasManyDeep function hasManyThrough(
required array relationships,
string relationMethodName,
boolean nested = variables._withoutRelationshipConstraints
boolean nested
) {
if ( arguments.relationships.len() <= 1 ) {
throw(
Expand All @@ -1715,6 +1715,7 @@ component accessors="true" {
}

param arguments.relationMethodName = lCase( callStackGet()[ 2 ][ "Function" ] );
arguments.nested = variables._withoutRelationshipConstraints == arguments.relationMethodName;

guardAgainstNotLoaded(
"This instance is not loaded so it cannot access the [#arguments.relationMethodName#] relationship. Either load the entity from the database using a query executor (like `first`) or base your query off of the [#arguments.relationships[ arguments.relationships.len() ]#] entity directly and use the `has` or `whereHas` methods to constrain it based on data in [#entityName()#]."
Expand All @@ -1726,11 +1727,11 @@ component accessors="true" {
var foreignKeys = [];
var localKeys = [];

var predecessor = this.clone( true );
var predecessor = this;
for ( var i = 1; i <= arguments.relationships.len(); i++ ) {
var relationName = arguments.relationships[ i ];
var relationship = predecessor.ignoreLoadedGuard( function() {
return predecessor.withoutRelationshipConstraints( function() {
return predecessor.withoutRelationshipConstraints( relationName, function() {
return invoke( predecessor, relationName );
} );
} );
Expand Down Expand Up @@ -1831,7 +1832,7 @@ component accessors="true" {
"parent" : this,
"relationships" : arguments.relationships,
"relationshipsMap" : relationshipsMap,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand Down Expand Up @@ -1904,7 +1905,7 @@ component accessors="true" {
"parent" : this,
"relationships" : arguments.relationships,
"relationshipsMap" : relationshipsMap,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand Down Expand Up @@ -1966,7 +1967,7 @@ component accessors="true" {
"type" : arguments.type,
"ids" : arguments.id,
"localKeys" : arguments.localKey,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand Down Expand Up @@ -2021,7 +2022,7 @@ component accessors="true" {
"foreignKeys" : arguments.id,
"localKeys" : [],
"type" : arguments.type,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand All @@ -2040,7 +2041,7 @@ component accessors="true" {
"foreignKeys" : arguments.id,
"localKeys" : arguments.localKey,
"type" : arguments.type,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand All @@ -2050,7 +2051,7 @@ component accessors="true" {
required array through,
required array foreignKeys,
required array localKeys,
boolean nested = variables._withoutRelationshipConstraints,
boolean nested = false,
string relationMethodName
) {
param arguments.relationMethodName = lCase( callStackGet()[ 2 ][ "Function" ] );
Expand Down Expand Up @@ -2117,7 +2118,7 @@ component accessors="true" {
"foreignKeys" : arguments.foreignKeys,
"localKeys" : arguments.localKeys,
"nested" : arguments.nested,
"withConstraints" : !variables._withoutRelationshipConstraints
"withConstraints" : variables._withoutRelationshipConstraints != arguments.relationMethodName
}
);
}
Expand Down
14 changes: 7 additions & 7 deletions models/QuickBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ component accessors="true" transientCache="false" {
var relationshipName = listFirst( column, "." );
if ( isNull( q ) ) {
q = getEntity().ignoreLoadedGuard( function() {
return getEntity().withoutRelationshipConstraints( function() {
return getEntity().withoutRelationshipConstraints( relationshipName, function() {
return invoke( getEntity(), relationshipName ).addCompareConstraints();
} );
} );
Expand All @@ -159,7 +159,7 @@ component accessors="true" transientCache="false" {
.ignoreLoadedGuard( function() {
return q
.getEntity()
.withoutRelationshipConstraints( function() {
.withoutRelationshipConstraints( relationshipName, function() {
return invoke( q.getEntity(), relationshipName );
} );
} );
Expand Down Expand Up @@ -261,7 +261,7 @@ component accessors="true" transientCache="false" {
}

var countBuilder = getEntity().ignoreLoadedGuard( function() {
return getEntity().withoutRelationshipConstraints( function() {
return getEntity().withoutRelationshipConstraints( relationName, function() {
return invoke( getEntity(), relationName )
.addCompareConstraints()
.when( true, callback )
Expand Down Expand Up @@ -322,7 +322,7 @@ component accessors="true" transientCache="false" {
}

var sumBuilder = getEntity().ignoreLoadedGuard( function() {
return getEntity().withoutRelationshipConstraints( function() {
return getEntity().withoutRelationshipConstraints( relationName, function() {
var related = invoke( getEntity(), relationName );
return related
.addCompareConstraints()
Expand Down Expand Up @@ -564,7 +564,7 @@ component accessors="true" transientCache="false" {
}
var currentRelationship = listFirst( arguments.relationName, "." );
var relation = getEntity().ignoreLoadedGuard( function() {
return getEntity().withoutRelationshipConstraints( function() {
return getEntity().withoutRelationshipConstraints( currentRelationship, function() {
return invoke( getEntity(), currentRelationship );
} );
} );
Expand Down Expand Up @@ -602,7 +602,7 @@ component accessors="true" transientCache="false" {
var thisRelationshipName = listFirst( arguments.relationshipName, "." );
if ( isNull( q ) ) {
q = getEntity().ignoreLoadedGuard( function() {
return getEntity().withoutRelationshipConstraints( function() {
return getEntity().withoutRelationshipConstraints( thisRelationshipName, function() {
return invoke( getEntity(), thisRelationshipName ).addCompareConstraints().clearOrders();
} );
} );
Expand All @@ -612,7 +612,7 @@ component accessors="true" transientCache="false" {
.ignoreLoadedGuard( function() {
return q
.getEntity()
.withoutRelationshipConstraints( function() {
.withoutRelationshipConstraints( thisRelationshipName, function() {
return invoke( q.getEntity(), thisRelationshipName );
} );
} );
Expand Down
20 changes: 12 additions & 8 deletions models/QuickQB.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,9 @@ component
boolean negate = false
) {
var relation = getEntity().ignoreLoadedGuard( function() {
return getEntity().withoutRelationshipConstraints( function() {
return invoke( getEntity(), listFirst( relationshipName, "." ) );
var relationName = listFirst( relationshipName, "." );
return getEntity().withoutRelationshipConstraints( relationName, function() {
return invoke( getEntity(), relationName );
} );
} );

Expand Down Expand Up @@ -511,10 +512,11 @@ component
var relation = relationQuery
.getEntity()
.ignoreLoadedGuard( function() {
var relationName = listFirst( relationshipName, "." );
return relationQuery
.getEntity()
.withoutRelationshipConstraints( function() {
return invoke( relationQuery.getEntity(), listFirst( relationshipName, "." ) );
.withoutRelationshipConstraints( relationName, function() {
return invoke( relationQuery.getEntity(), relationName );
} );
} );

Expand Down Expand Up @@ -583,8 +585,9 @@ component
boolean negate = false
) {
var relation = getEntity().ignoreLoadedGuard( function() {
return getEntity().withoutRelationshipConstraints( function() {
return invoke( getEntity(), listFirst( relationshipName, "." ) );
var relationName = listFirst( relationshipName, "." );
return getEntity().withoutRelationshipConstraints( relationName, function() {
return invoke( getEntity(), relationName );
} );
} );

Expand Down Expand Up @@ -682,10 +685,11 @@ component
var relation = arguments.relationQuery
.getEntity()
.ignoreLoadedGuard( function() {
var relationName = listFirst( relationshipName, "." );
return relationQuery
.getEntity()
.withoutRelationshipConstraints( function() {
return invoke( relationQuery.getEntity(), listFirst( relationshipName, "." ) );
.withoutRelationshipConstraints( relationName, function() {
return invoke( relationQuery.getEntity(), relationName );
} );
} );

Expand Down
2 changes: 1 addition & 1 deletion tests/resources/app/models/User.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ component extends="quick.models.BaseEntity" accessors="true" {
qb.addSubselect(
"latestPostId",
this.ignoreLoadedGuard( function() {
return this.withoutRelationshipConstraints( function() {
return this.withoutRelationshipConstraints( "posts", function() {
return this
.posts()
.addCompareConstraints()
Expand Down

0 comments on commit dff52d6

Please sign in to comment.