A hasManyDeep relationship is either a one-to-many or a many-to-many relationship. It is used when you want to access related entities through one or more intermediate entities. For example, you may want to retrieve all the blog posts for a Team:
// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function posts() {
return hasManyDeep(
relationName = "Post",
through = [ "User" ],
foreignKeys = [
"teamId", // the key on User that refers to Team
"authorId" // the key on Post that refers to User
],
localKeys = [
"id", // the key on Team that identifies Team
"id" // the key on User that identifies User
]
);
}
}
If a mapping in the through array is found as a WireBox mapping, it will be treated as a Pivot Table.
If you are traversing a polymorhpic relationship, pass an array as the key type where the first key points to the polymorphic type column and the second key points to the identifying value.
Constraining Relationships
There are two options for constraining a HasManyDeep relationship.
The first option is by using table aliases.
// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function activePosts() {
return hasManyDeep(
relationName = "Post",
through = [ "User AS u" ],
foreignKeys = [
"teamId", // the key on User that refers to Team
"authorId" // the key on Post that refers to User
],
localKeys = [
"id", // the key on Team that identifies Team
"id" // the key on User that identifies User
]
).where( "u.active", 1 );
}
}
If you want to use scopes or avoid table aliases, you can use callback functions to constrain the relationship:
// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function activePosts() {
return hasManyDeep(
relationName = "Post",
through = [ () => newEntity( "User ).active() ],
foreignKeys = [
"teamId", // the key on User that refers to Team
"authorId" // the key on Post that refers to User
],
localKeys = [
"id", // the key on Team that identifies Team
"id" // the key on User that identifies User
]
);
}
}
A mapping name to the final related entity, a function that produces a QuickBuilder or Relationship instance, or a QuickBuilder or Relationship instance.
through
Array<String | Function | QuickBuilder | IRelationship>
The entities to traverse through from the parent (current) entity to get to the relatedEntity.
Each item in the array can be either a mapping name to the final related entity, a function that produces a QuickBuilder or Relationship instance, or a QuickBuilder or Relationship instance.
foreignKeys
Array<String>
An array of foreign keys traversing to the relatedEntity.
The length of this array should be one more than the length of through.
The first key of this array would be the column on the first through entity that refers back to the parent (current) entity, and so forth.
localKeys
Array<String>
An array of local keys traversing to the relatedEntity.
The length of this array should be one more than the length of through.
The first key of this array would be the column on the parent (current) entity that identifies it, and so forth.
nested
boolean
false
Signals the relationship that it is currently being resolved as part of another hasManyDeep relationship. This is handled by the framework when using a hasManyThrough relationship.
relationMethodName
String
Current Method Name
The method name called to retrieve this relationship. Uses a stack backtrace to determine by default.