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...
// User.cfc
component extends="quick.models.BaseEntity" {}component quick {}// User.cfc
component extends="quick.models.BaseEntity" {
function profile() {
return hasOne( "UserProfile" );
}
}return hasOne("UserProfile", "FK_userID");// User.cfc
component table="my_users" extends="quick.models.BaseEntity" {}// User.cfc
component extends="quick.models.BaseEntity" {
variables._key = "user_id";
}// User.cfc
component extends="quick.models.BaseEntity" {
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" {
property name="id";
property name="username";
property name="email";
}// User.cfc// User.cfc
component extends="quick.models.BaseEntity" {
property name="bcrypt" inject="@BCrypt" persistent="false";
property name="id";
property name="username";
property name="email";
}component extends="quick.models.BaseEntity" {
property name="id";
property name="username" column="user_name";
property name="countryId" column="FK_country_id";
}// User.cfc
component datasource="myOtherDatasource" grammar="PostgresGrammar" extends="quick.models.BaseEntity" {}return belongsTo("UserProfile", "FK_userID", "profile_id");// UserProfile.cfc
component extends="quick.models.BaseEntity" {
function user() {
return belongsTo( "User" );
}
}instanceReady Lifecycle Method{
"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'}"
}component extends="quick.models.BaseEntity" {
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 );
}
}component {
function index( event, rc, prc ) {
return getInstance( "User" ).all();
}
}component extends="quick.models.BaseEntity" {
property name="bcrypt" inject="@BCrypt";
function setPassword( value ) {
return assignAttribute( "password", bcrypt.value );
}
function getCreatedDate( value ) {
return dateFormat( retrieveAttribute( "createdDate" ), "DD MMM YYYY" );
}
}function keyType() {
return variables._wirebox.getInstance( "Sequence@quick" )
.setSequenceName( "seq_users" );
}component {
property name="userService" inject="quickService:User";
}{
"id" = 1,
"username" = "JaneDoe",
"email" = "[email protected]",
"createdDate" = "03/12/2018",
"modifiedDate" = "03/12/2018"
}[
{
"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" {
function user() {
return belongsTo( "User" );
}
}var user = getInstance( "User" ).find( 1 );
user.update( {
email = "[email protected]",
password = "newpassword"
} );getInstance( "User" )
.whereActive( false )
.deleteAll();getInstance( "User" ).deleteAll( [ 4, 10, 22 ] );var posts = user.getPosts();var newestPosts = user
.posts()
.orderBy( "publishedDate", "desc" )
.get();ablecommentablecommentable_typecommentable_idComment_type_id// Post.cfc
component extends="quick.models.BaseEntity" {
function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}
}// Video.cfc
component extends="quick.models.BaseEntity" {
function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}
}ArrayReduceCFCollection// User.cfc
component extends="quick.models.BaseEntity" {
function permissions() {
return hasManyThrough( "Permission" );
}
}return belongsTo("User", "FK_userID");return belongsTo("User", "FK_userID", "relatedPostId");// User.cfc
component extends="quick.models.BaseEntity" {
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 );getInstance( "User" )
.where( "lastLoggedIn", ">", dateAdd( "m", 3, now() ) )
.updateAll( {
"active" = 0
} );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)
.orderBy("username", "desc")
.limit(10)
.get();component {
property name="userService" inject="quickService:User"
}var users = userService
.where("active", 1)
.orderBy("username", "desc")
.limit(10)
.get();moduleSettings = {
quick = {
defaultGrammar = "MySQLGrammar"
}
};// Comment.cfc
component extends="quick.models.BaseEntity" {
function post() {
return polymorphicBelongsTo( "commentable" );
}
}// 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 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();// Permission.cfc
component extends="quick.models.BaseEntity" {
function users() {
return hasManyThrough( "User" );
}
}// UserPermission.cfc
component extends="quick.models.BaseEntity" {
function user() {
return belongsTo( "User" );
}
function permission() {
return belongsTo( "Permission" );
}
}return hasManyThrough("Permission", "UserPermission", "FK_permissionID");return hasManyThrough(
"Permission",
"UserPermission",
"FK_permissionID",
"FK_userID"
);return hasManyThrough(
relationName = "Permission",
intermeediateName = "UserPermission",
firstKey = "FK_permissionID", // foreign key on the UserPermission table
secondKey = "FK_userID", // foreign key on the Permission table
localKey = "userID", // local key on the owning entity table
secondLocalKey = "id" // local key on the UserPermission table
);// Comment.cfc
component extends="quick.models.BaseEntity" {
function post() {
return polymorphicBelongsTo( "commentable" );
}
}var user = getInstance( "User" );
user.setUsername( "JaneDoe" );
user.setEmail( "[email protected]" );
user.setPassword( "mypass1234" );
user.save();// Post.cfc
component extends="quick.models.BaseEntity" {
function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}
}var user = getInstance( "User" ).create( {
"username" = "JaneDoe",
"email" = "[email protected]",
"password" = "mypass1234"
} );// Video.cfc
component extends="quick.models.BaseEntity" {
function comments() {
return polymorphicHasMany( "Comment", "commentable" );
}
}// User.cfc
component extends="quick.models.BaseEntity" {
function posts() {
return hasMany( "Post" );
}
}return hasMany("Post", "FK_userID");return hasMany("Post", "FK_userID", "relatedPostId");// Post.cfc
component extends="quick.models.BaseEntity" {
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 ] );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 query
.whereBetween( "published_date", rc.startDate, rc.endDate )
.with( { "comments" = function( q2 ) {
return q2.where( "body", "like", rc.search );
} } );
} } ).latest().get();// User.cfc
component extends="quick.models.BaseEntity" {
function permissions() {
return belongsToMany( "Permission" );
}
}// Permission.cfc
component extends="quick.models.BaseEntity" {
function users() {
return belongsToMany( "User" );
}
}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" {
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" {
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" {
function scopeOfType( query, type ) {
return query.where( "type", type );
}
}var subscribedUsers = getInstance( "User" )
.ofType( "admin" )
.get();component extends="User" table="users" {
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" {
/* properties */
function logins() {
return hasMany( "Login" );
}
function scopeWithLastLoginDate( query ) {
addSubselect( "lastLoginDate", newEntity( "Login" )
.select( "timestamp" )
.whereColumn( "users.id", "user_id" )
.latest()
);
}
}var user = getInstance( "User" ).withLastLoginDate().first();
user.getLastLoginDate(); // {ts 2019-05-02 08:24:51}component extends="BaseEntity" {
/* 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.
}permissions_users
- permissionId
- userId// User.cfc
component extends="quick.models.BaseEntity" {
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" {
function user() {
belongsToMany( "User", "user_permission_map", "FK_PermissionID", "FK_UserId" );
}
}// User.cfc
component extends="quick.models.BaseEntity" {
function userPermissions() {
return hasMany( "UserPermission" );
}
function permissions() {
return hasManyThrough( "Permission", "UserPermission" );
}
}var post = getInstance("Post").findOrFail(1);
var tag = getInstance("Tag").create("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").create("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 );