Quick ORM
Search…
Retrieving Entities
Once you have an entity and its associated database table you can start retrieving data from your database.
You can configure your query to retrieve entities using any qb method. It is highly recommended you become familiar with the qb documentation.

Active Record

You start every interaction with Quick with an instance of an entity. The easiest way to do this is using WireBox. getInstance is available in all handlers by default. WireBox can easily be injected in to any other class you need using inject="wirebox".
Quick is backed by qb, a CFML Query Builder. With this in mind, think of retrieving records for your entities like interacting with qb. For example:
1
var users = getInstance( "User" ).all();
2
3
for ( var user in users ) {
4
writeOutput( user.getUsername() );
5
}
Copied!
In addition to using for you can utilize the each function on arrays. For example:
1
var users = getInstance( "User" ).all();
2
3
prc.users.each( function( user ) {
4
writeOutput( user.getUsername() );
5
});
Copied!
You can add constraints to the query just the same as you would using qb directly:
1
var users = getInstance( "User" )
2
.where( "active", 1 )
3
.orderByDesc( "username" )
4
.limit( 10 )
5
.get();
Copied!
For more information on what is possible with qb, check out the qb documentation.

Quick Service

A second way to retrieve results is to use a Quick Service. It is similar to a VirtualEntityService from cborm.
The easiest way to create a Quick Service is to inject it using the quickService: dsl:
1
component {
2
3
property name="userService" inject="quickService:User"
4
5
}
Copied!
If you have a existing Service, and you would like to extend the quickService, you can extend the quikc.models.BaseService and then call super.init inside of the service init function passing the name of the entity (for example your User Entity) shown below:
1
component singleton extends="quick.models.BaseService" {
2
3
function init(){
4
super.init( "User" );
5
}
6
7
}
Copied!
Any method you can call on an entity can be called on the service. A new entity will be used for all calls to a Quick Service.
1
var users = userService
2
.where( "active", 1 )
3
.orderByDesc( "username" )
4
.limit( 10 )
5
.get();
Copied!
If you need a new unsaved entity from your service, you can call myService.newEntity()

Aggregates

Calling qb's aggregate methods (count, max, etc.) will return the appropriate value instead of an entity or collection of entities.

existsOrFail

Name
Type
Required
Default
Description
id
any
false
An optional id to check if it exists.
errorMessage
any
false
"No [#entityName()#] found with id [#arguments.id#]"
An optional string error message or callback to produce a string error message. If a callback is used, it is passed the unloaded entity as the only argument.
Returns true if any entities exist with the configured query. If no entities exist, it throws an EntityNotFound exception.
1
var doesUserExist = getInstance( "User" )
2
.existsOrFail( rc.userID );
Copied!

Retrieval Methods

all

Name
Type
Required
Default
Description
No arguments
``
Retrieves all the records for an entity. Calling all will ignore any non-global constraints on the query.
1
var users = getInstance( "User" ).all();
Copied!

get

Name
Type
Required
Default
Description
No arguments
``
Executes the configured query, eager loads any relations, and returns the entities in a new collection.
1
var posts = getInstance( "Post" )
2
.whereNotNull( "publishedDate" )
3
.get();
Copied!

paginate

Name
Type
Required
Default
Description
page
numeric
false
1
The page of results to return.
maxRows
numeric
false
25
The number of rows to return.
Executes the configured query, eager loads any relations, and returns the entities in the configured qb pagination struct.
1
var posts = getInstance( "Post" )
2
.whereNotNull( "publishedDate" )
3
.paginate( rc.page, rc.maxrows );
Copied!
1
// default response example
2
{
3
"results": [ User#1, User#2, ... ],
4
"pagination": {
5
"totalPages": 2,
6
"maxRows": 25,
7
"offset": 0,
8
"page": 1,
9
"totalRecords": 40
10
}
11
}
Copied!

simplePaginate

Name
Type
Required
Default
Description
page
numeric
false
1
The page of results to return.
maxRows
numeric
false
25
The number of rows to return.
Executes the configured query, eager loads any relations, and returns the entities in the configured qb simple pagination struct.
1
var posts = getInstance( "Post" )
2
.whereNotNull( "publishedDate" )
3
.simplePaginate( rc.page, rc.maxrows );
Copied!
1
// default response example
2
{
3
"results": [ User#1, User#2, ... ],
4
"pagination": {
5
"hasMore": true,
6
"maxRows": 25,
7
"offset": 0,
8
"page": 1
9
}
10
}
Copied!

first

Name
Type
Required
Default
Description
No arguments
``
Executes the configured query and returns the first entity found. If no entity is found, returns null.
1
var user = getInstance( "User" )
2
.where( "username", rc.username )
3
.first();
Copied!

firstWhere

Name
Type
Required
Default
Description
column
any
true
The name of the column with which to constrain the query. A closure can be passed to begin a nested where statement.
operator
any
true
The operator to use for the constraint (i.e. "=", "<", ">=", etc.). A value can be passed as the operator and the value left null as a shortcut for equals (e.g. where( "column", 1 ) == where( "column", "=", 1 ) ).
value
any
false
The value with which to constrain the column. An expression (builder.raw()) can be passed as well.
combinator
string
false
"and"
The boolean combinator for the clause (e.g. "and" or "or"). Default: "and"
Adds a basic where clause to the query and returns the first result.
1
var user = getInstance( "User" )
2
.firstWhere( "username", rc.username );
Copied!

firstOrFail

Name
Type
Required
Default
Description
errorMessage
any
false
"No [#entityName()#] found with constraints [#serializeJSON( retrieveQuery().getBindings() )#]"
An optional string error message or callback to produce a string error message. If a callback is used, it is passed the unloaded entity as the only argument.
Executes the configured query and returns the first entity found. If no entity is found, then an EntityNotFound exception is thrown with the given or default error message.
1
var user = getInstance( "User" )
2
.where( "username", rc.username )
3
.firstOrFail();
Copied!

firstOrNew

Name
Type
Required
Default
Description
attributes
struct
false
{}
A struct of attributes to restrict the query. If no entity is found the attributes are filled on the new entity returned.
newAttributes
struct
false
{}
A struct of attributes to fill on the new entity if no entity is found. These attributes are combined with attributes.
ignoreNonExistentAttributes
boolean
false
false
If true, does not throw an exception if an attribute does not exist. Instead, it skips the non-existent attribute.
Finds the first matching record or returns an unloaded new entity.
1
var user = getInstance( "User" )
2
.firstOrNew( { "username": rc.username } );
Copied!

firstOrCreate

Name
Type
Required
Default
Description
attributes
struct
false
{}
A struct of attributes to restrict the query. If no entity is found the attributes are filled on the new entity created.
newAttributes
struct
false
{}
A struct of attributes to fill on the created entity if no entity is found. These attributes are combined with attributes.
ignoreNonExistentAttributes
boolean
false
false
If true, does not throw an exception if an attribute does not exist. Instead, it skips the non-existent attribute.
Finds the first matching record or creates a new entity.
1
var user = getInstance( "User" )
2
.firstOrCreate( { "username": rc.username } );
Copied!

find

Name
Type
Required
Default
Description
id
any
true
The id value to find.
Returns the entity with the id value as the primary key. If no record is found, it returns null instead.
1
var user = getInstance( "User" )
2
.find( rc.userID );
Copied!

findOrFail

Name
Type
Required
Default
Description
id
any
true
The id value to find.
errorMessage
any
false
"No [#entityName()#] found with id [#arguments.id#]"
An optional string error message or callback to produce a string error message. If a callback is used, it is passed the unloaded entity as the only argument.
Returns the entity with the id value as the primary key. If no record is found, it throws an EntityNotFound exception.
1
var user = getInstance( "User" )
2
.findOrFail( rc.userID );
Copied!

findOrNew

Name
Type
Required
Default
Description
id
any
true
The id value to find.
attributes
struct
false
{}
A struct of attributes to fill on the new entity if no entity is found.
ignoreNonExistentAttributes
boolean
false
false
If true, does not throw an exception if an attribute does not exist. Instead, it skips the non-existent attribute.
Returns the entity with the id value as the primary key. If no record is found, it returns a new unloaded entity.
1
var user = getInstance( "User" ).findOrNew(
2
9999,
3
{
4
"firstName" : "doesnt",
5
"lastName" : "exist"
6
}
7
);
Copied!

findOrCreate

Name
Type
Required
Default
Description
id
any
true
The id value to find.
attributes
struct
false
{}
A struct of attributes to use when creating the new entity if no entity is found.
ignoreNonExistentAttributes
boolean
false
false
If true, does not throw an exception if an attribute does not exist. Instead, it skips the non-existent attribute.
Returns the entity with the id value as the primary key. If no record is found, it returns a newly created entity.
1
var user = getInstance( "User" ).findOrCreate(
2
9999,
3
{
4
"username" : "doesntexist",
5
"firstName" : "doesnt",
6
"lastName" : "exist",
7
"password" : "secret"
8
}
9
);
Copied!

newEntity

Name
type
required
default
description
name
string
wirebox name of the quick entity
You will probably not use newEntity very often if you use wirebox getinstance("someQuickEntity") to retrieve an entity, because that will already give you a new entity. If you are using a QuickService you will need this method for creating a new entity
1
var user = getInstance( "User" )
2
3
//or
4
5
property name="UserService" inject="quickService:User"; //inject a quick service
6
7
var user = UserService.newEntity();
Copied!

Hydration Methods

Hydration is a term to describe filling an entity with a struct of data and then marking it as loaded, without doing any database queries. For example, this might be useful when hydrating a user from session data instead of doing a query every request.

hydrate

Name
Type
Required
Default
Description
attributes
struct
false
{}
A struct of key / value pairs to fill in to the entity.
ignoreNonExistentAttributes
boolean
false
false
If true, does not throw an exception if an attribute does not exist. Instead, it skips the non-existent attribute.
Hyrdates an entity from a struct of data. Hydrating an entity fills the entity and then marks it as loaded.
If the entity's keys are not included in the struct of data, a MissingHydrationKey is thrown.
1
var user = getInstance( "User" ).hydrate( {
2
"id": 4,
3
"username": "JaneDoe",
4
"email": "[email protected]",
5
"password": "mypass1234"
6
} );
7
8
user.isLoaded(); // true
Copied!

hydrateAll

Name
Type
Required
Default
Description
mementos
array
false
[]
An array of structs to hydrate into entities.
ignoreNonExistentAttributes
boolean
false
false
If true, does not throw an exception if an attribute does not exist. Instead, it skips the non-existent attribute.
Hydrates a new collection of entities from an array of structs.
1
var users = getInstance( "User" ).hydrateAll( [
2
{
3
"id": 3,
4
"username": "JohnDoe",
5
"email": "[email protected]",
6
"password": "mypass4321"
7
},
8
{
9
"id": 4,
10
"username": "JaneDoe",
11
"email": "[email protected]",
12
"password": "mypass1234"
13
}
14
] );
Copied!

Custom Collections

If you would like collections of entities to be returned as something besides an array, you can override the newCollection method. It receives the array of entities. You can return any custom collection you desire.

newCollection

Name
Type
Required
Default
Description
entities
array
false
[]
The array of entities returned by the query.
Returns a new collection of the given entities. It is expected to override this method in your entity if you need to specify a different collection to return. You can also call this method with no arguments to get an empty collection.
1
// Post.cfc
2
component extends="quick.models.BaseEntity" accessors="true" {
3
4
function newCollection( array entities = [] ) {
5
return variables._wirebox.getInstance(
6
name = "extras.QuickCollection",
7
initArguments = {
8
"collection" = arguments.entities
9
}
10
);
11
}
12
13
}
Copied!
Last modified 5mo ago