A belongsTo relationship is a many-to-one relationship. For instance, a Post may belong to a User.
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:
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.
The inverse of belongsTo is or .
To update a belongsTo relationship, use the associate method. associate takes the entity to associate as the only argument.
Note:
associatedoes not automatically save the entity. Make sure to callsavewhen you are ready to persist your changes to the database.
To remove a belongsTo relationship, use the dissociate method.
Note:
dissociatedoes not automatically save the entity. Make sure to callsavewhen you are ready to persist your changes to the database.
You can also influence the associated entities by calling "set" & relationshipName and passing in an entity or key value.
After executing this code, the post would be updated in the database to be associated with the user with an id of 1.
// Post.cfc
component extends="quick.models.BaseEntity" {
function user() {
return belongsTo( "User" );
}
}return belongsTo("User", "FK_userID");return belongsTo("User", "FK_userID", "relatedPostId");// User.cfc
component extends="quick.models.BaseEntity" {
function posts() {
return hasMany( "Post" );
}
function latestPost() {
// remember, relationships are just queries!
return hasOne( "Post" ).orderBy( "createdDate", "desc" );
}
}var post = getInstance("Post").findOrFail(1);
var user = getInstance("User").findOrFail(1);
post.user().associate(user);
post.save();var post = getInstance("Post").findOrFail(1);
post.user().dissociate();
post.save();var post = getInstance( "Post" ).first();
post.setAuthor( 1 );