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

Custom Getters & Setters

Sometimes you want to use a different value in your code than is stored in your database. Perhaps you want to enforce that setting a password always is hashed with BCrypt. Maybe you have a Date value object that you want wrapping each of your dates. You can accomplish this using custom getters and setters.

A custom getter or setter is simply a function in your entity.

To retrieve the attribute value fetched from the database, call getAttribute passing in the name of the attribute.

To set an attribute for saving to the database, call setAttribute passing in the name and the value.

component extends="quick.models.BaseEntity" {

    property name="bcrypt" inject="@BCrypt";

    function setPassword( value ) {
        return setAttribute( "password", bcrypt.value );
    }

    function getCreatedDate( value ) {
        return dateFormat( getAttribute( "createdDate" ), "DD MMM YYYY" );
    } 

}

Note: Custom getters and setters with not be called when hydrating a model from the database.

PreviousCollectionsNextSerialization

Last updated 7 years ago

Was this helpful?