Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
// 2017_11_10_122835_create_users_table.cfc
component {
function up() {
schema.create( "users", function( table ) {
table.increments( "id" );
table.string( "username" ).unique();
table.string( "email" ).unique();
table.string( "password" );
table.timestamp( "createdDate" );
table.timestamp( "updatedDate" );
} );
}
}// User
component extends="quick.models.BaseEntity" {
// the name of the table is the pluralized version of the model
// all fields in a table are mapped by default
// both of these points can be configured on a per-entity basis
}// handlers/Users.cfc
component {
// /users/:id
function show( event, rc, prc ) {
// this finds the User with an id of 1 and retrieves it
prc.user = getInstance( "User" ).findOrFail( rc.id );
event.setView( "users/show" );
}
}<!-- views/users/show.cfm -->
<cfoutput>
<h1>Hi, #prc.user.getUsername()#!</h1>
</cfoutput>var posts = user.getPosts();var newestPosts = user
.posts()
.orderBy( "publishedDate", "desc" )
.get();// This will only find posts the user has written.
var post = user.posts().findOrFail( rc.id );component extends="quick.models.BaseEntity" accessors="true" {
property name="bcrypt" inject="@BCrypt";
function setPassword( value ) {
return assignAttribute( "password", bcrypt.hashPassword( value ) );
}
function getCreatedDate( value ) {
return dateFormat( retrieveAttribute( "createdDate" ), "DD MMM YYYY" );
}
}moduleSettings = {
quick = {
defaultGrammar = "MySQLGrammar@qb"
}
};var user = getInstance( "User" ).find( 1 );
user.delete();getInstance( "User" )
.whereActive( false )
.deleteAll();getInstance( "User" ).deleteAll( [ 4, 10, 22 ] );accessors="true"accessors="true"get loads and executes the relationship query.component name="User" {
function newCollection( array entities = [] ) {
return variables._wirebox.getInstance(
name = "quick.extras.QuickCollection",
initArguments = {
"collection" = arguments.entities
}
);
}
}var users = getInstance("User").all();
users
.filter(function(user) {
return user.getActive();
})
.pluck("username")
.groupBy(function(username) {
return left(username, 1);
});var posts = getInstance("Post").all();
if (someCondition) {
posts.load("user");
}getInstance("Post")
.with("user")
.all();getInstance( "Post" )
.where( "userId", prc.loggedInUser.getId() )
.findOrFail( rc.id );prc.loggedInUser.posts().findOrFail( rc.id );// User.cfc
component extends="quick.models.BaseEntity" {
function posts() {
return hasMany( "Post" );
}
function publishedPosts() {
return this.posts().whereNotNull( "publishedDate" );
}
}// Post.cfc
component extends="quick.models.BaseEntity" {
function scopePublished( qb ) {
qb.whereNotNull( "publishedDate" );
}
}// User.cfc
component extends="quick.models.BaseEntity" {
function posts() {
return hasMany( "Post" );
}
function publishedPosts() {
return this.posts().published();
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function posts() {
return hasMany( "Post" );
}
}return hasMany( "Post", "FK_userID" );return hasMany( "Post", "FK_userID", "relatedPostId" );// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function user() {
return belongsTo( "User" );
}
}var post = getInstance( "Post" ).create( {
"title" = "My Post",
"body" = "Hello, world!"
} );
var user = getInstance( "User" ).findOrFail( 1 );
user.posts().save( post );
// OR use the keyValue
user.posts().save( post.keyValue() );var user = getInstance( "User" ).findOrFail( 1 );
user.posts().create( {
"title" = "My Post",
"body" = "Hello, world!"
} );var postA = getInstance( "Post" ).findOrFail( 2 );
user.setPosts( [ postA, 4 ] );// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function profile() {
return hasOne( "UserProfile" );
}
}// Comment.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function post() {
return polymorphicBelongsTo( "commentable" );
}
}// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}
}// Video.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}
}return hasOne( "UserProfile", "FK_userID" );return belongsTo( "UserProfile", "FK_userID", "profile_id" );// UserProfile.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function user() {
return belongsTo( "User" );
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function profile() {
return hasOne( "UserProfile" ).withDefault();
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function profile() {
return hasOne( "UserProfile" ).withDefault( {
"showHints": true
} );
}
}var userQuery = getInstance( "User" )
.where( "active", "=", 1 );
writeOutput( userQuery.toSQL() );SELECT `users`.`id`, `users`.`username` /* etc */
FROM "users"
WHERE "active" = ?var userQuery = getInstance( "User" )
.where( "active", "=", 1 );
writeOutput( userQuery.toSQL( showBindings = true ) );SELECT `users`.`id`, `users`.`username` /* etc */
FROM "users"
WHERE "active" = {"value":1,"cfsqltype":"CF_SQL_NUMERIC","null":false}getInstance( "User" )
.tap( function( e ) {
writeOutput( e.toSQL() & "<br>" );
} )
.where( "active", "=", 1 )
.tap( function( e ) {
writeOutput( e.toSQL() & "<br>" );
} );SELECT `users`.`id`, `users`.`username` /* etc */ FROM "users"
SELECT `users`.`id`, `users`.`username` /* etc */ FROM "users" WHERE "active" = ?logbox = {
debug = [ "qb.models.Grammars" ]
};component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="username";
property name="email";
property name="password";
property name="createdDate";
property name="modifiedDate";
}{
"id" = 1,
"username" = "JaneDoe",
"email" = "[email protected]",
"password" = "$2a$04$2nVI5rPOfl6.hrflkhBWOObO5Z7lXGJpi1vlosY74NrL/CKdpWqZS"
"createdDate" = "{ts '2018-03-12 16:14:10'}",
"modifiedDate" = "{ts '2018-03-12 16:14:10'}"
}function instanceReady() {
this.memento = {
"defaultIncludes" : retrieveAttributeNames( withVirtualAttributes = true ),
"defaultExcludes" : [],
"neverInclude" : [],
"defaults" : {},
"mappers" : {},
"trustedGetters" : true,
"ormAutoIncludes" : false
};
}struct function getMemento(
includes = "", // or []
excludes = "", // or []
struct mappers = {},
struct defaults = {},
boolean ignoreDefaults = false,
boolean trustedGetters
)component extends="quick.models.BaseEntity" accessors="true" {
property name="id";
property name="username";
property name="email";
property name="password";
property name="createdDate";
property name="modifiedDate";
function getMemento() {
return {
id = getId(),
username = getUsername(),
email = getEmail(),
createdDate = dateFormat( getCreatedDate(), "MM/DD/YYYY" ),
// can also use getAttribute if you want to bypass a custom getter
modifiedDate = dateFormat( retrieveAttribute( "modifiedDate" ), "MM/DD/YYYY" )
};
}
}{
"id" = 1,
"username" = "JaneDoe",
"email" = "[email protected]",
"createdDate" = "03/12/2018",
"modifiedDate" = "03/12/2018"
}component {
// /users/:id
function show( event, rc, prc ) {
return getInstance( "User" ).findOrFail( rc.id );
}
}{
"id" = 1,
"username" = "JaneDoe",
"email" = "[email protected]",
"createdDate" = "03/12/2018",
"modifiedDate" = "03/12/2018"
}component {
function index( event, rc, prc ) {
return getInstance( "User" ).all();
}
}[
{
"id" = 1,
"username" = "JaneDoe",
"email" = "[email protected]",
"createdDate" = "03/12/2018",
"modifiedDate" = "03/12/2018"
},
{
"id" = 2,
"username" = "JohnDoe",
"email" = "[email protected]",
"createdDate" = "03/14/2018",
"modifiedDate" = "03/15/2018"
}
]// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}
}// Video.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}
}// Comment.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function post() {
return polymorphicBelongsTo( "commentable" );
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function permissions() {
return belongsToMany( "Permission" );
}
}// Permission.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function users() {
return belongsToMany( "User" );
}
}permissions_users
- permissionId
- userId// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function permissions() {
return belongsToMany( "Permission", "user_permission_map" );
}
}return belongsToMany(
"Permission",
"user_permission_map",
"FK_UserId",
"FK_PermissionID"
);return belongsToMany(
"Permission",
"user_permission_map",
"FK_UserId",
"FK_PermissionID",
"user_id",
"permission_id"
);// Permission.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function user() {
belongsToMany( "User", "user_permission_map", "FK_PermissionID", "FK_UserId" );
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function userPermissions() {
return hasMany( "UserPermission" );
}
function permissions() {
return hasManyThrough( "Permission", "UserPermission" );
}
}var post = getInstance( "Post" ).findOrFail( 1 );
var tag = getInstance( "Tag" ).create( { "name": "miscellaneous" });
// pass an id
post.tags().attach( tag.getId() );
// or pass an entity
post.tags().attach( tag );var post = getInstance( "Post" ).findOrFail( 1 );
var tag = getInstance("Tag").firstWhere( "name", "miscellaneous" );
// pass an id
post.tags().detach( tag.getId() );
// or pass an entity
post.tags().detach( tag );var post = getInstance( "Post" ).findOrFail( 1 );
post.tags().sync( [ 2, 3, 6 ] );var someTag = getInstance( "Tag" ).findOrFail( 2 );
var post = getInstance( "Post" ).first();
post.setTags( [ 4, 12, someTag );var user = getInstance( "User" ).find( 1 );
user.setPassword( "newpassword" );
user.save();
Postvar 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// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function latestPost() {
return hasOneThrough( [ "members", "posts" ] )
.orderByDesc( "publishedDate" );
}
function members() {
return hasMany( "User" );
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function posts() {
return hasMany( "Post" );
}
function team() {
return belongsTo( "Team" );
}
}// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function author() {
return belongsTo( "Post" );
}
}hasOneThrough( [ "members", "posts" ] );
+----------------+---------------------------+----------------+
| Current Entity | Relationship Method Names | Related Entity |
+================+===========================+================+
| Team | members | User |
+----------------+---------------------------+----------------+
| User | posts | Post |
+----------------+---------------------------+----------------+// Office.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function latestPost() {
return hasOneThrough( [ "teams", "members", "posts" ] )
.orderByDesc( "publishedDate" );
}
function teams() {
return hasMany( "Team" );
}
}// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function members() {
return hasMany( "User" );
}
function office() {
return belongsTo( "Office" );
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function team() {
return belongsTo( "Team" );
}
function posts() {
return hasMany( "Post" );
}
}// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function author() {
return belongsTo( "User" );
}
}hasOneThrough( [ "teams", "members", "posts" ] )
+----------------+---------------------------+----------------+
| Current Entity | Relationship Method Names | Related Entity |
+================+===========================+================+
| Office | teams | Team |
+----------------+---------------------------+----------------+
| Team | members | User |
+----------------+---------------------------+----------------+
| User | posts | Post |
+----------------+---------------------------+----------------+// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function latestPost() {
return hasOneThrough( [ "members", "posts" ] )
.orderByDesc( "publishedDate" )
.withDefault();
}
function members() {
return hasMany( "User" );
}
}// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function latestPost() {
return hasOneThrough( [ "members", "posts" ] )
.orderByDesc( "publishedDate" )
.withDefault( {
"title": "Your next great post!"
} );
}
function members() {
return hasMany( "User" );
}
}Permission// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function permissions() {
return hasManyThrough( [ "userPermissions", "permission" ] );
}
function userPermissions() {
return hasMany( "UserPermission" );
}
}// Permission.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function users() {
return hasManyThrough( [ "userPermissions", "user" ] );
}
function userPermissions() {
return hasMany( "UserPermission" );
}
}// UserPermission.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function user() {
return belongsTo( "User" );
}
function permission() {
return belongsTo( "Permission" );
}
}hasManyThrough( [ "userPermissions", "permission" ] );
+----------------+---------------------------+----------------+
| Current Entity | Relationship Method Names | Related Entity |
+================+===========================+================+
| User | userPermissions | UserPermission |
+----------------+---------------------------+----------------+
| UserPermission | permission | Permission |
+----------------+---------------------------+----------------+// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function team() {
return belongsTo( "Team" );
}
function teammates() {
return hasManyThrough( [ "team", "members" ] );
}
}// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function members() {
return hasMany( "User" );
}
}hasManyThrough( [ "team", "members" ] );
+----------------+---------------------------+----------------+
| Current Entity | Relationship Method Names | Related Entity |
+================+===========================+================+
| User | team | Team |
+----------------+---------------------------+----------------+
| Team | members | User |
+----------------+---------------------------+----------------+// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function team() {
return belongsTo( "Team" );
}
function teammates() {
return hasManyThrough( [ "team", "members" ] );
}
function officemates() {
return hasManyThrough( [ "team", "office", "teams", "members" ] );
}
}// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function members() {
return hasMany( "User" );
}
function office() {
return belongsTo( "Office" );
}
}// Office.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function teams() {
return hasMany( "Team" );
}
}hasManyThrough( [ "team", "office", "teams", "members" ] );
+----------------+---------------------------+----------------+
| Current Entity | Relationship Method Names | Related Entity |
+================+===========================+================+
| User | team | Team |
+----------------+---------------------------+----------------+
| Team | office | Office |
+----------------+---------------------------+----------------+
| Office | teams | Team |
+----------------+---------------------------+----------------+
| Team | members | User |
+----------------+---------------------------+----------------+// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function team() {
return belongsTo( "Team" );
}
function teammates() {
return hasManyThrough( [ "team", "members" ] );
}
function officemates() {
return hasManyThrough( [ "team", "office", "members" ] );
}
}// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function members() {
return hasMany( "User" );
}
function office() {
return belongsTo( "Office" );
}
}// Office.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function teams() {
return hasMany( "Team" );
}
function members() {
return hasManyThrough( [ "teams", "members" ] );
}
}hasManyThrough( [ "team", "office", "members" ] );
+----------------+---------------------------+----------------+
| Current Entity | Relationship Method Names | Related Entity |
+================+===========================+================+
| User | team | Team |
+----------------+---------------------------+----------------+
| Team | office | Office |
+----------------+---------------------------+----------------+
| Office | members | (below) |
+----------------+---------------------------+----------------+---+
| Office | teams | Team |
+----------------+---------------------------+----------------+
| Team | members | User |
+----------------+---------------------------+----------------+// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {}// User.cfc
component table="my_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 ); // falsegetInstance( "User" ).has( "posts" ).get();getInstance( "User" ).has( "posts", ">", 2 ).get();getInstance( "User" ).has( "posts.comments" ).get();getInstance( "User" ).doesntHave( "posts" ).get();getInstance( "User" ).doesntHave( "posts", "<=", 1 ).get();getInstance( "User" ).doesntHave( "posts.comments" ).get();getInstance( "User" )
.whereHas( "posts", function( q ) {
q.where( "body", "like", "%different%" );
} )
.get();getInstance( "User" )
.whereHas( "posts.comments", function( q ) {
q.where( "body", "like", "%great%" );
} )
.get();getInstance( "User" )
.whereHas( "posts.comments", function( q ) {
q.where( "body", "like", "%great%" );
}, ">", 2 )
.get();getInstance( "User" )
.whereDoesntHave( "posts", function( q ) {
q.where( "body", "like", "%different%" );
} )
.get();getInstance( "User" )
.whereDoesntHave( "posts.comments", function( q ) {
q.where( "body", "like", "%great%" );
} )
.get();getInstance( "User" )
.whereDoesntHave( "posts.comments", function( q ) {
q.where( "body", "like", "%great%" );
}, ">", 2 )
.get();getInstance( "Post" ).orderBy( "author.name" );getInstance( "Post" ).orderBy( "author.team.name" );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() {
this.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( query ) {
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( query, required date afterDate ) {
return query.where( "lastLoginDate", ">", afterDate );
}component extends="quick.models.BaseEntity" accessors="true" {
/* properties */
function logins() {
return hasMany( "Login" );
}
function scopeAddLastLoginDate( query ) {
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( query ) {
return 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" );
}
}getInstance( "Post" ).orderByRelated( "author.team", "name" );
// or
getInstance( "Post" ).orderByRelated( [ "author", "team" ], "name" );prc.posts = getInstance( "Post" ).limit( 25 ).get():<cfoutput>
<h1>Posts</h1>
<ul>
<cfloop array="#prc.posts#" item="post">
<li>#post.getTitle()# by #post.getAuthor().getUsername()#</li>
</cfloop>
</ul>
</cfoutput>prc.posts = getInstance( "Post" )
.with( "author" )
.limit( 25 )
.get();// Post.cfc
component extends="quick.models.BaseEntity" {
function author() {
return belongsTo( "User" );
}
}getInstance( "Post" ).with( "author" ).get();SELECT * FROM `posts` LIMIT 25
SELECT * FROM `users` WHERE `id` IN (1, 2, 3, 4, 5, 6, ...)// User.cfc
component extends="quick.models.BaseEntity" {
function country() {
return belongsTo( "User" );
}
}getInstance( "Post" ).with( "author.country" );getInstance( "Post" ).with( [ "author.country", "tags" ] );// User.cfc
component {
function posts() {
return hasMany( "Post" );
}
function publishedPosts() {
return hasMany( "Post" ).published(); // published is a query scope on Post
}
}getInstance( "User" ).with( "posts" ).get();
getInstance( "User" ).with( "publishedPosts" ).get();getInstance( "User" ).with( { "posts" = function( query ) {
} } ).latest().get();getInstance( "User" ).with( { "posts" = function( q1 ) {
return q1
.whereBetween( "published_date", rc.startDate, rc.endDate )
.with( { "comments" = function( q2 ) {
return q2.where( "body", "like", rc.search );
} } );
} } ).latest().get();fill method.belongsToinstanceReady Lifecycle Methodvar user = getInstance( "User" );
user.setUsername( "JaneDoe" );
user.setEmail( "[email protected]" );
user.setPassword( "mypass1234" );
user.save();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" )
.firstOrNew( { "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"
}
] );// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function user() {
return belongsTo( "User" );
}
}return belongsTo( "User", "FK_userID" );return belongsTo( "User", "FK_userID", "relatedPostId" );// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function posts() {
return hasMany( "Post" );
}
function latestPost() {
// remember, relationships are just queries!
return hasOne( "Post" ).orderBy( "createdDate", "desc" );
}
}var post = getInstance( "Post" ).findOrFail(1);
var user = getInstance( "User" ).findOrFail(1);
post.user().associate( user );
post.save();var post = getInstance( "Post" ).findOrFail(1);
post.user().dissociate();
post.save();var post = getInstance( "Post" ).first();
post.setAuthor( 1 );// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function user() {
return belongsTo( "User" ).withDefault();
}
}// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function user() {
return belongsTo( "User" ).withDefault( {
"firstName": "Guest",
"lastName": "Author"
} );
}
}keyColumn : String -> keyColumns : [String]getExistenceCompareKey : String -> getExistenceCompareKeys : [String]ownerKey : String -> ownerKeys : [String]relatedPivotKey : String -> relatedPivotKeys : [String]relatedlocalKey : String -> localKeys : [String]ownerKey : String -> ownerKeys : [String]localKey : String -> localKeys : [String]moduleSettings = {
"quick": {
"defaultGrammar": "MSSQLGrammar@qb"
}
};component extends="quick.models.BaseEntity" {
function posts() {
return hasManyThrough( "Post", "User", "country_id", "user_id" );
}
}component extends="quick.models.BaseEntity" {
function posts() {
return hasManyThrough( [ "users", "posts" ] );
}
function users() {
return hasMany( "User" );
}
}component extends="quick.models.BaseEntity" {
function posts() {
return hasMany( "Post" );
}
}var newChild = getInstance( "Child" );
newChild.fill( fields );
newChild.parent().associate( parent );
newChild.save();var newChild = getInstance( "Child" );
newChild.fill( fields );
newChild.setParentKey( parent.getId() );
newChild.save();var newChild = getInstance( "Child" );
newChild.fill( fields );
var parent = getInstance( "Parent" ).findOrFail( id );
parent.children().save( newChild );var parent = getInstance( "Parent" ).findOrFail( id );
var newChild = parent.children().create( fields );function keyType() {
return variables._wirebox.getInstance( "Sequence@quick" )
.setSequenceName( "seq_users" );
}component {
property name="userService" inject="quickService:User";
}var 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"
}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 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" )
.firstOrNew( { "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" ).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
}
);
}
}Team// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function team() {
return belongsToThrough( [ "author", "team" ] );
}
function author() {
return belongsTo( "Post" );
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function posts() {
return hasMany( "Post" );
}
function team() {
return belongsTo( "Team" );
}
}// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function members() {
return hasMany( "User" );
}
}belongsToThrough( [ "author", "team" ] );
+----------------+---------------------------+----------------+
| Current Entity | Relationship Method Names | Related Entity |
+================+===========================+================+
| Post | author | User |
+----------------+---------------------------+----------------+
| User | team | Team |
+----------------+---------------------------+----------------+// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function office() {
return belongsToThrough( [ "author", "team", "office" ] );
}
function author() {
return belongsTo( "Post" );
}
}// User.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function posts() {
return hasMany( "Post" );
}
function team() {
return belongsTo( "Team" );
}
}// Team.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function members() {
return hasMany( "User" );
}
function office() {
return belongsTo( "Office" );
}
}// Office.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function teams() {
return hasMany( "Team" );
}
}belongsToThrough( [ "author", "team", "office" ] );
+----------------+---------------------------+----------------+
| Current Entity | Relationship Method Names | Related Entity |
+================+===========================+================+
| Post | author | User |
+----------------+---------------------------+----------------+
| User | team | Team |
+----------------+---------------------------+----------------+
| Team | office | Office |
+----------------+---------------------------+----------------+// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function team() {
return belongsToThrough( [ "author", "team" ] ).withDefault();
}
function author() {
return belongsTo( "Post" );
}
}// Post.cfc
component extends="quick.models.BaseEntity" accessors="true" {
function team() {
return belongsToThrough( [ "author", "team" ] ).withDefault( {
"name": "No Team"
} );
}
function author() {
return belongsTo( "Post" );
}
}