Quick ORM
Search…
belongsToThrough

Usage

A belongsToThrough relationship is the one side of a many-to-one relationship with an intermediate entity. It is used when you want to access a related, owning entity through one or more intermediate entities. For instance, a Post may belong to a Team via a User.
1
// Post.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function team() {
5
return belongsToThrough( [ "author", "team" ] );
6
}
7
8
function author() {
9
return belongsTo( "Post" );
10
}
11
12
}
Copied!
1
// User.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function posts() {
5
return hasMany( "Post" );
6
}
7
8
function team() {
9
return belongsTo( "Team" );
10
}
11
12
}
Copied!
1
// Team.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function members() {
5
return hasMany( "User" );
6
}
7
8
}
Copied!
The only value needed for belongsToThrough is an array of relationship function names to walk through to get to the related entity. The first relationship function name in the array must exist on the current entity. Each subsequent function name must exist on the related entity of the previous relationship result. For our previous example, author must be a relationship function on Post. team must then be a relationship function on the related entity resulting from calling Post.author(). This returns a belongsTo relationship where the related entity is User. So, User must have a team relationship function. That is the end of the relationship function names array, so the related entity resulting from calling User.team() is our final entity which is Team.
1
belongsToThrough( [ "author", "team" ] );
2
3
+----------------+---------------------------+----------------+
4
| Current Entity | Relationship Method Names | Related Entity |
5
+================+===========================+================+
6
| Post | author | User |
7
+----------------+---------------------------+----------------+
8
| User | team | Team |
9
+----------------+---------------------------+----------------+
Copied!
This approach can scale to as many related entities as you need. For instance, let's expand the previous example to include an Office that houses many Teams.
1
// Post.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function office() {
5
return belongsToThrough( [ "author", "team", "office" ] );
6
}
7
8
function author() {
9
return belongsTo( "Post" );
10
}
11
12
}
Copied!
1
// User.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function posts() {
5
return hasMany( "Post" );
6
}
7
8
function team() {
9
return belongsTo( "Team" );
10
}
11
12
}
Copied!
1
// Team.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function members() {
5
return hasMany( "User" );
6
}
7
8
function office() {
9
return belongsTo( "Office" );
10
}
11
12
}
Copied!
1
// Office.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function teams() {
5
return hasMany( "Team" );
6
}
7
8
}
Copied!
1
belongsToThrough( [ "author", "team", "office" ] );
2
3
+----------------+---------------------------+----------------+
4
| Current Entity | Relationship Method Names | Related Entity |
5
+================+===========================+================+
6
| Post | author | User |
7
+----------------+---------------------------+----------------+
8
| User | team | Team |
9
+----------------+---------------------------+----------------+
10
| Team | office | Office |
11
+----------------+---------------------------+----------------+
Copied!

withDefault

HasOneThrough relationships can be configured to return a default entity if no entity is found. This is done by calling withDefault on the relationship object.
1
// Post.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function team() {
5
return belongsToThrough( [ "author", "team" ] ).withDefault();
6
}
7
8
function author() {
9
return belongsTo( "Post" );
10
}
11
12
}
Copied!
Called this way will return a new unloaded entity with no data. You can also specify any default attributes data by passing in a struct of data to withDefault.
1
// Post.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function team() {
5
return belongsToThrough( [ "author", "team" ] ).withDefault( {
6
"name": "No Team"
7
} );
8
}
9
10
function author() {
11
return belongsTo( "Post" );
12
}
13
14
}
Copied!

Signature

Name
Type
Required
Default
Description
relationships
array
true
An array of relationship function names. The relationships are resolved from left to right. Each relationship will be resolved from the previously resolved relationship, starting with the current entity.
relationMethodName
string
false
Current Method Name
The method name called to retrieve this relationship. Uses a stack backtrace to determine by default.
Last modified 1yr ago
Copy link
Edit on GitHub