moduleSettings = {
quick = {
defaultGrammar = "MySQLGrammar@qb"
}
};var user = getInstance( "User" );
user.isLoaded(); // false
user.save();
user.isLoaded(); // true

component
table="media"
extends="quick.models.BaseEntity"
accessors="true"
{
property name="id";
property name="uploadFileName";
property name="fileLocation";
property name="fileSizeBytes";
}component
extends="Media"
table="book_media"
joinColumn="FK_media"
accessors="true"
{
property name="displayOrder";
property name="designation";
function approvalStatus(){
return belongsTo( "Book", "FK_book" );
}
}var coverPhotos = getInstance( "BookMedia" )
.where( "designation", "cover" )
.orderBy( "displayOrder", "ASC" );var smallCoverPhotos = getInstance( "BookMedia" )
.where( "designation", "cover" )
.where( "fileSizeBytes", "<", 40000 )
.orderBy( "displayOrder", "ASC" )
.orderBy( "uploadFileName", "ASC" );var myBookMediaItem = getInstance( "BookMedia" ).get( myId );function media(){
return hasMany( "BookMedia", "FK_book" ).orderBy( "displayOrder", "ASC" );
}// Media (parent entity)
component
extends="quick.models.BaseEntity"
accessors="true"
table="media"
discriminatorColumn="type" // the database column that determines the subclass
{
property name="id";
property name="uploadFileName";
property name="fileLocation";
property name="fileSizeBytes";
// Array of all possible subclass entities
variables._discriminators = [
"BookMedia"
];
}// BookMedia (subclass)
component
extends="Media"
accessors="true"
discriminatorValue="book" // column value unique to this subclass
{
property name="displayOrder";
property name="designation";
}// BookMedia (subclass entity using MTI)
component
extends="Media"
table="media_book" // table for BookMedia data
joinColumn="mediaId" // column to join on
accessors="true"
{// Media (parent entity)
component
extends="quick.models.BaseEntity"
accessors="true"
table="media"
discriminatorColumn="type"
singleTableInheritance="true" // Enable STI
{var bookMedia = getInstance( "Media" ).where( "type", "book" ).get();var allMedia = getInstance( "Media" ).all();var newBookMedia = getInstance( "Media" ).newChildEntity( "BookMedia" );getInstance( "User" )
.whereActive( false )
.deleteAll();getInstance( "User" ).deleteAll( [ 4, 10, 22 ] );var user = getInstance( "User" ).find( 1 );
user.setPassword( "newpassword" );
user.save();var user = getInstance( "User" ).find( 1 );
user.update( {
email = "[email protected]",
password = "newpassword"
} );var user = getInstance( "User" ).find( 1 );
user.update( rc, true );var user = getInstance( "User" ).updateOrCreate( {
"username": "newuser"
} );getInstance( "User" )
.where( "lastLoggedIn", ">", dateAdd( "m", 3, now() ) )
.updateAll( {
"active" = 0
} );var user = getInstance( "User" ).findOrFail( rc.userID );
var sameUser = user.fresh();var user = getInstance( "User" ).findOrFail( rc.userID );
user.refresh(); // user now has updated data from the database// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {}// User.cfc
component table="t_users" extends="quick.models.BaseEntity" accessors="true" {}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
variables._key = "user_id";
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function keyType() {
return variables._wirebox.getInstance( "UUIDKeyType@quick" );
}
}interface displayname="KeyType" {
/**
* Called to handle any tasks before inserting into the database.
* Recieves the entity as the only argument.
*/
public void function preInsert( required entity );
/**
* Called to handle any tasks after inserting into the database.
* Recieves the entity and the queryExecute result as arguments.
*/
public void function postInsert( required entity, required struct result );
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="username";
property name="email";
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
property name="bcrypt" inject="@BCrypt" persistent="false";
property name="id";
property name="username";
property name="email";
}component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="username" column="user_name";
property name="countryId" column="FK_country_id";
}component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="number" convertToNull="false";
property name="title" nullValue="REALLY_NULL";
}component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="createdDate" readonly="true";
}component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="number" sqltype="cf_sql_varchar";
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="active" casts="BooleanCast@quick";
}// BooleanCast.cfc
component implements="CastsAttribute" {
/**
* Casts the given value from the database to the target cast type.
*
* @entity The entity with the attribute being casted.
* @key The attribute alias name.
* @value The value of the attribute.
*
* @return The casted attribute.
*/
public any function get(
required any entity,
required string key,
any value
) {
return isNull( arguments.value ) ? false : booleanFormat( arguments.value );
}
/**
* Returns the value to assign to the key before saving to the database.
*
* @entity The entity with the attribute being casted.
* @key The attribute alias name.
* @value The value of the attribute.
*
* @return The value to save to the database. A struct of values
* can be returned if the cast value affects multiple attributes.
*/
public any function set(
required any entity,
required string key,
any value
) {
return arguments.value ? 1 : 0;
}
}// Address.cfc
component accessors="true" {
property name="streetOne";
property name="streetTwo";
property name="city";
property name="state";
property name="zip";
function fullStreet() {
var street = [ getStreetOne(), getStreetTwo() ];
return street.filter( function( part ) {
return !isNull( part ) && part != "";
} ).toList( chr( 10 ) );
}
function formatted() {
return fullStreet() & chr( 10 ) & "#getCity()#, #getState()# #getZip()#";
}
}component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="username";
property name="firstName" column="first_name";
property name="lastName" column="last_name";
property name="password";
property name="address"
casts="AddressCast"
persistent="false"
getter="false"
setter="false";
property name="streetOne";
property name="streetTwo";
property name="city";
property name="state";
property name="zip";
}component implements="quick.models.Casts.CastsAttribute" {
property name="wirebox" inject="wirebox";
/**
* Casts the given value from the database to the target cast type.
*
* @entity The entity with the attribute being casted.
* @key The attribute alias name.
* @value The value of the attribute.
* @attributes The struct of attributes for the entity.
*
* @return The casted attribute.
*/
public any function get(
required any entity,
required string key,
any value
) {
return wirebox.getInstance( dsl = "Address" )
.setStreetOne( entity.retrieveAttribute( "streetOne" ) )
.setStreetTwo( entity.retrieveAttribute( "streetTwo" ) )
.setCity( entity.retrieveAttribute( "city" ) )
.setState( entity.retrieveAttribute( "state" ) )
.setZip( entity.retrieveAttribute( "zip" ) );
}
/**
* Returns the value to assign to the key before saving to the database.
*
* @entity The entity with the attribute being casted.
* @key The attribute alias name.
* @value The value of the attribute.
* @attributes The struct of attributes for the entity.
*
* @return The value to save to the database. A struct of values
* can be returned if the cast value affects multiple attributes.
*/
public any function set(
required any entity,
required string key,
any value
) {
return {
"streetOne": arguments.value.getStreetOne(),
"streetTwo": arguments.value.getStreetTwo(),
"city": arguments.value.getCity(),
"state": arguments.value.getState(),
"zip": arguments.value.getZip()
};
}
}component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="email" column="email" update="false" insert="true";
}// User.cfc
component
datasource="myOtherDatasource"
grammar="PostgresGrammar@qb"
extends="quick.models.BaseEntity"
accessors="true"
{
// ....
}var userOne = getInstance( "User" ).findOrFail( 1 );
var userTwo = getInstance( "User" ).findOrFail( 1 );
userOne.isSameAs( userTwo ); // true
userOne.isNotSameAs( userTwo ); // falsevar users = getInstance( "User" ).all();
for ( var user in users ) {
writeOutput( user.getUsername() );
}var users = getInstance( "User" ).all();
prc.users.each( function( user ) {
writeOutput( user.getUsername() );
});var users = getInstance( "User" )
.where( "active", 1 )
.orderByDesc( "username" )
.limit( 10 )
.get();component {
property name="userService" inject="quickService:User"
}component singleton extends="quick.models.BaseService" {
function init(){
super.init( "User" );
}
}var users = userService
.where( "active", 1 )
.orderByDesc( "username" )
.limit( 10 )
.get();var doesUserExist = getInstance( "User" )
.existsOrFail( rc.userID );var users = getInstance( "User" ).all();var posts = getInstance( "Post" )
.whereNotNull( "publishedDate" )
.get();var posts = getInstance( "Post" )
.whereNotNull( "publishedDate" )
.paginate( rc.page, rc.maxrows );// default response example
{
"results": [ User#1, User#2, ... ],
"pagination": {
"totalPages": 2,
"maxRows": 25,
"offset": 0,
"page": 1,
"totalRecords": 40
}
}var posts = getInstance( "Post" )
.whereNotNull( "publishedDate" )
.simplePaginate( rc.page, rc.maxrows );// default response example
{
"results": [ User#1, User#2, ... ],
"pagination": {
"hasMore": true,
"maxRows": 25,
"offset": 0,
"page": 1
}
}var user = getInstance( "User" )
.where( "username", rc.username )
.first();var user = getInstance( "User" )
.firstWhere( "username", rc.username );var user = getInstance( "User" )
.where( "username", rc.username )
.firstOrFail();var user = getInstance( "User" )
.firstOrNew( { "username": rc.username } );var user = getInstance( "User" )
.firstOrCreate( { "username": rc.username } );var user = getInstance( "User" )
.find( rc.userID );var user = getInstance( "User" )
.findOrFail( rc.userID );var user = getInstance( "User" ).findOrNew(
9999,
{
"firstName" : "doesnt",
"lastName" : "exist"
}
);var user = getInstance( "User" ).findOrCreate(
9999,
{
"username" : "doesntexist",
"firstName" : "doesnt",
"lastName" : "exist",
"password" : "secret"
}
);var user = getInstance( "User" )
//or
property name="UserService" inject="quickService:User"; //inject a quick service
var user = UserService.newEntity();var user = getInstance( "User" ).hydrate( {
"id": 4,
"username": "JaneDoe",
"email": "[email protected]",
"password": "mypass1234"
} );
user.isLoaded(); // truevar users = getInstance( "User" ).hydrateAll( [
{
"id": 3,
"username": "JohnDoe",
"email": "[email protected]",
"password": "mypass4321"
},
{
"id": 4,
"username": "JaneDoe",
"email": "[email protected]",
"password": "mypass1234"
}
] );// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function newCollection( array entities = [] ) {
return variables._wirebox.getInstance(
name = "extras.QuickCollection",
initArguments = {
"collection" = arguments.entities
}
);
}
}var user = getInstance( "User" );
user.setUsername( "JaneDoe" );
user.setEmail( "[email protected]" );
user.setPassword( "mypass1234" );
user.save();ignoreNonExistentAttributes is false which is the default).var user = getInstance( "User" );
user.fill( {
"username": "JaneDoe",
"email": "[email protected]",
"password": "mypass1234"
} );
user.save();var user = getInstance( "User" ).create( {
"username": "JaneDoe",
"email": "[email protected]",
"password": "mypass1234"
} );var user = getInstance( "User" )
.firstOrNew( { "username": rc.username } );var user = getInstance( "User" )
.firstOrCreate( { "username": rc.username } );var user = getInstance( "User" ).findOrNew(
9999,
{
"firstName" : "doesnt",
"lastName" : "exist"
}
);var user = getInstance( "User" ).findOrCreate(
9999,
{
"username" : "doesntexist",
"firstName" : "doesnt",
"lastName" : "exist",
"password" : "secret"
}
);var user = getInstance( "User" ).updateOrCreate( {
"username": "newuser"
} );var user = getInstance( "User" ).hydrate( {
"id": 4,
"username": "JaneDoe",
"email": "[email protected]",
"password": "mypass1234"
} );
user.isLoaded(); // truevar users = getInstance( "User" ).hydrateAll( [
{
"id": 3,
"username": "JohnDoe",
"email": "[email protected]",
"password": "mypass4321"
},
{
"id": 4,
"username": "JaneDoe",
"email": "[email protected]",
"password": "mypass1234"
}
] );var subscribedUsers = getInstance( "User" )
.where( "subscribed", true )
.orderBy( "subscribedDate" )
.get();var subscribedUsers = getInstance( "User" )
.where( "subscribed", true )
.get();var subscribedUsers = getInstance( "User" )
.whereNotNull( "subscribedDate" )
.get();component extends="quick.models.BaseEntity" accessors="true" {
function scopeSubscribed( query ) {
return query.where( "subscribed", true );
}
}var subscribedUsers = getInstance( "User" )
.subscribed()
.get();var subscribedUsers = getInstance( "User" )
.subscribed()
.orderBy( "subscribedDate" )
.get();component extends="quick.models.BaseEntity" accessors="true" {
function scopeLongestSubscribers( query ) {
return query.orderBy( "subscribedDate" );
}
function scopeSubscribed( query ) {
return query.where( "subscribed", true );
}
}var subscribedUsers = getInstance( "User" )
.subscribed()
.longestSubscribers()
.get();component extends="quick.models.BaseEntity" accessors="true" {
function scopeOfType( query, type ) {
return query.where( "type", type );
}
}var subscribedUsers = getInstance( "User" )
.ofType( "admin" )
.get();component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="username";
property name="password";
property name="type";
function scopeOfType( query, type = "limited" ) {
return query.where( "type", type );
}
function scopeResetPasswords( query ) {
return query.updateAll( { "password" = "" } ).result.recordcount;
}
}getInstance( "User" ).ofType( "admin" ).resetPasswords(); // 1component extends="User" table="users" accessors="true" {
function applyGlobalScopes( qb ) {
qb.ofType( "admin" );
}
}var admins = getInstance( "Admin" ).all();
// SELECT * FROM users WHERE type = 'admin'var admins = getInstance( "Admin" ).withoutGlobalScope( [ "ofType" ] ).all();
// SELECT * FROM userscomponent extends="quick.models.BaseEntity" accessors="true" {
/* properties */
function logins() {
return hasMany( "Login" ).latest();
}
function scopeAddLastLoginDate( qb ) {
qb.addSubselect( "lastLoginDate", newEntity( "Login" )
.select( "timestamp" )
.whereColumn( "users.id", "user_id" )
);
}
}var user = getInstance( "User" ).addLastLoginDate().first();
user.getLastLoginDate(); // {ts 2019-05-02 08:24:51} var user = getInstance( "User" )
.addLastLoginDate()
.where( "lastLoginDate", ">", "2019-05-10" )
.all();function scopeLoggedInAfter( qb, required date afterDate ) {
return qb.where( "lastLoginDate", ">", afterDate );
}component extends="quick.models.BaseEntity" accessors="true" {
/* properties */
function logins() {
return hasMany( "Login" );
}
function scopeAddLastLoginDate( qb ) {
qb.addSubselect( "lastLoginDate", "logins.timestamp" );
}
}var user = getInstance( "User" )
.addSubselect( "lastLoginDate", "logins.timestamp" )
.first();
user.getLastLoginDate(); // {ts 2019-05-02 08:24:51}component extends="quick.models.BaseEntity" accessors="true" {
/* properties */
function scopeWithLatestPost( qb ) {
return qb.addSubselect( "latestPostId", newEntity( "Post" )
.select( "id" )
.whereColumn( "user_id", "users.id" )
.orderBy( "created_date", "desc" )
).with( "latestPost" );
}
function latestPost() {
return belongsTo( "Post", "latestPostId" );
}
}var users = getInstance( "User" ).withLatestPost().all();
for ( var user in users ) {
user.getLatestPost().getTitle(); // My awesome post, etc.
}// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function scopeAddType( qb ) {
qb.selectRaw( "
CASE
WHEN publishedDate IS NULL THEN 'unpublished'
ELSE 'published'
END AS publishedStatus
" );
appendVirtualAttribute( "publishedStatus" );
}
}