Quick ORM
Search…
polymorphicBelongsTo

Usage

A polymorphicBelongsTo relationship is a many-to-one relationship. This relationship is used when an entity can belong to multiple types of entities. The classic example for this type of relationship is Posts, Videos, and Comments. For instance, a Comment may belong to a Post or a Video.
1
// Comment.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function post() {
5
return polymorphicBelongsTo( "commentable" );
6
}
7
8
}
Copied!
The only value passed to polymorphicBelongsTo is a prefix for the polymorphic type. A common convention where is to add able to the end of the entity name, though this is not automatically done. In our example, this prefix is commentable. This tells quick to look for a commentable_type and a commentable_id column in our Comment entity. It stores our entity's mapping as the _type and our entity's primary key value as the _id.
When retrieving a polymorphicBelongsTo relationship the _id is used to retrieve a _type from the database.
The inverse of polymorphicBelongsTo is also polymorphicHasMany. It is important to choose the right relationship for your database structure. hasOne assumes that the related model has the foreign key for the relationship.
1
// Post.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function comments() {
5
return polymorphicHasMany( "Comment", "commentable" );
6
}
7
8
}
Copied!
1
// Video.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function comments() {
5
return polymorphicHasMany( "Comment", "commentable" );
6
}
7
8
}
Copied!

Signature

Name
Type
Required
Default
Description
name
String
false
relationMethodName
The name given to the polymorphic relationship.
type
String
false
name & "_type"
The column name that defines the type of the polymorphic relationship.
id
String
false
name & "_id"
The column name that defines the id of the polymorphic relationship.
localKey
String | [String]
false
related.keyNames()
The column name on the realted entity that is referred to by the foreignKey of 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 polymorphicBelongsTo relationship between this entity and the entity defined by relationName.

Visualizer

Last modified 1yr ago
Copy link
Edit on GitHub