Quick ORM
Search…
belongsTo

Usage

A belongsTo relationship is a many-to-one relationship. For instance, a Post may belong to a User.
1
// Post.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function user() {
5
return belongsTo( "User" );
6
}
7
8
}
Copied!
The first value passed to belongsTo is a WireBox mapping to the related entity.
Quick determines the foreign key of the relationship based on the entity name and key values. In this case, the Post entity is assumed to have a userId foreign key. You can override this by passing a foreign key in as the second argument:
1
return belongsTo( "User", "FK_userID" );
Copied!
If your parent entity does not use id as its primary key, or you wish to join the child entity to a different column, you may pass a third argument to the belongsTo method specifying your parent table's custom key.
1
return belongsTo( "User", "FK_userID", "relatedPostId" );
Copied!
The inverse of belongsTo is hasMany or hasOne.
1
// User.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function posts() {
5
return hasMany( "Post" );
6
}
7
8
function latestPost() {
9
// remember, relationships are just queries!
10
return hasOne( "Post" ).orderBy( "createdDate", "desc" );
11
}
12
13
}
Copied!

Updating

To update a belongsTo relationship, use the associate method. associate takes the entity to associate as the only argument.
1
var post = getInstance( "Post" ).findOrFail(1);
2
var user = getInstance( "User" ).findOrFail(1);
3
post.user().associate( user );
4
post.save();
Copied!
associate does not automatically save the entity. Make sure to call save when you are ready to persist your changes to the database.

Removing

To remove a belongsTo relationship, use the dissociate method.
1
var post = getInstance( "Post" ).findOrFail(1);
2
post.user().dissociate();
3
post.save();
Copied!
dissociate does not automatically save the entity. Make sure to call save when you are ready to persist your changes to the database.

Relationship Setter

You can also influence the associated entities by calling "set" & relationshipName and passing in an entity or key value.
1
var post = getInstance( "Post" ).first();
2
post.setAuthor( 1 );
Copied!
After executing this code, the post would be updated in the database to be associated with the user with an id of 1.

withDefault

BelongsTo 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 user() {
5
return belongsTo( "User" ).withDefault();
6
}
7
8
}
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 user() {
5
return belongsTo( "User" ).withDefault( {
6
"firstName": "Guest",
7
"lastName": "Author"
8
} );
9
}
10
11
}
Copied!

Signature

Name
Type
Required
Default
Description
relationName
string
true
The WireBox mapping for the related entity.
foreignKey
String | [String]
false
entityName() & keyNames()
The foreign key on the parent entity.
localKey
String | [String]
false
keyNames()
The local primary key on the parent entity.
relationMethodName
String
false
The method name called on the entity to produce this relationship.
The method name called to retrieve this relationship. Uses a stack backtrace to determine by default.
DO NOT PASS A VALUE HERE UNLESS YOU KNOW WHAT YOU ARE DOING.
Returns a BelongsTo relationship between this entity and the entity defined by relationName.

Visualizer

Last modified 1yr ago