Quick ORM
1.3.0
1.3.0
  • Introduction
  • Getting Started
    • Defining An Entity
    • Retrieving Entities
    • Creating New Entities
    • Updating Existing Entities
    • Deleting Entities
    • Query Scopes
  • Relationships
    • Relationship Types
      • hasOne
      • hasMany
      • belongsTo
      • belongsToMany
      • hasManyThrough
      • polymorphicBelongsTo
      • polymorphicHasMany
    • Retrieving Relationships
    • Eager Loading
  • Collections
  • Custom Getters & Setters
  • Serialization
  • Interception Points
  • Debugging
  • Contributing
Powered by GitBook
On this page

Was this helpful?

Edit on Git
Export as PDF
  1. Relationships
  2. Relationship Types

hasOne

PreviousRelationship TypesNexthasMany

Last updated 7 years ago

Was this helpful?

Defining

A hasOne relationship is a "one-to-one" relationship. For instance, a User entity might have an UserProfile entity attached to it.

// User.cfc
component extends="quick.models.BaseEntity" {

    function profile() {
       return hasOne( "UserProfile" );
    }

}

The first value passed to hasOne 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 UserProfile entity is assumed to have a userId foreign key. You can override this by passing a foreign key in as the second argument:

return hasOne( "UserProfile", "FK_userID" );

The inverse of hasOne is . 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.

// UserProfile.cfc
component extends="quick.models.BaseEntity" {

    function user() {
        return belongsTo( "User" );
    }

}
belongsTo