Quick ORM
Search…
Defining An Entity
To get started with Quick, you need an entity. You start by extending quick.models.BaseEntity.
1
// User.cfc
2
component extends="quick.models.BaseEntity" {}
Copied!
That's all that is needed to get started with Quick. There are a few defaults of Quick worth mentioning here.

Tables

We don't need to tell Quick what table name to use for our entity. By default, Quick uses the pluralized name of the component for the table name. That means for our User entity Quick will assume the table name is users. You can override this by specifying a table metadata attribute on the component.
1
// User.cfc
2
component table="my_users" extends="quick.models.BaseEntity" {}
Copied!

Primary Key

By default, Quick assumes a primary key of id. The name of this key can be configured by setting variables._key in your component.
1
// User.cfc
2
component extends="quick.models.BaseEntity" {
3
4
variables._key = "user_id";
5
6
}
Copied!
Quick also assumes a key type that is auto-incrementing. If you would like a different key type, define a function called `keyType` and return the key type from that function.
1
// User.cfc
2
component extends="quick.models.BaseEntity" {
3
4
function keyType() {
5
return variables._wirebox.getInstance( "[email protected]" );
6
}
7
8
}
Copied!
Quick ships with the following key types:
  • AutoIncrementingKeyType
  • NullKeyType
  • ReturningKeyType
  • UUIDKeyType
keyType can be any component that adheres to the keyType interface, so feel free to create your own and distribute them via ForgeBox.
1
interface displayname="KeyType" {
2
3
/**
4
* Called to handle any tasks before inserting into the database.
5
* Recieves the entity as the only argument.
6
*/
7
public void function preInsert( required entity );
8
9
/**
10
* Called to handle any tasks after inserting into the database.
11
* Recieves the entity and the queryExecute result as arguments.
12
*/
13
public void function postInsert( required entity, required struct result );
14
15
}
Copied!

Columns

You specify what columns are retrieved by adding properties to your component.
1
// User.cfc
2
component extends="quick.models.BaseEntity" {
3
4
property name="id";
5
property name="username";
6
property name="email";
7
8
}
Copied!
Now, only the id, username, and email columns will be retrieved.
Note: Make sure to include the primary key (id by default) as a property.
To prevent Quick from mapping a property to the database add the persistent="false" attribute to the property.
1
// User.cfc
Copied!
1
// User.cfc
2
component extends="quick.models.BaseEntity" {
3
4
property name="bcrypt" inject="@BCrypt" persistent="false";
5
6
property name="id";
7
property name="username";
8
property name="email";
9
10
}
Copied!
If the column name in your table is not the column name you wish to use in quick, you can alias it using the column metadata attribute.
1
component extends="quick.models.BaseEntity" {
2
3
property name="id";
4
property name="username" column="user_name";
5
property name="countryId" column="FK_country_id";
6
7
}
Copied!

Multiple datasource support

Quick uses a default datasource and default grammar, as described here. If you are using multiple datasources you can override default datasource by specifying a datasource metadata attribute on the component. If your extra datasource has a different grammar you can override your grammar as well by specifying a grammar attribute.
1
// User.cfc
2
component datasource="myOtherDatasource" grammar="PostgresGrammar" extends="quick.models.BaseEntity" {}
Copied!
At the time of writing Valid grammar options are: MySQLGrammar, PostgresGrammar, MSSQLGrammar and OracleGrammar. Please check the qb docs for additional options.
Last modified 2yr ago