Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Post// 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" );
}
}getInstance( "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();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 );belongsToCommentPostVideo// 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" );
}
}// 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"
} );
}
}// 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" );
}
}// 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" );
}
}return hasOne( "UserProfile", "FK_userID" );return hasOne( "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
} );
}
}// 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( [ "UserPermissions", "Permission" ] );
}
}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 post = getInstance( "Post" )
.withCount( "comments" )
.findOrFail( 1 );
post.getCommentsCount();var post = getInstance( "Post" )
.withCount( "comments AS myCommentsCount" )
.findOrFail( 1 );
post.getMyCommentsCount();var post = getInstance( "Post" )
.withCount( [
"comments AS allCommentsCount",
{ "comments AS pendingCommentsCount": function( q ) {
q.where( "approved", 0 );
} },
{ "comments AS approvedCommentsCount": function( q ) {
q.where( "approved", 1 );
} }
] )
.findOrFail( 1 );
post.getAllCommentsCount();
post.getPendingCommentsCount();
post.getApprovedCommentsCount();getInstance( "Post" ).orderBy( "author.name" );getInstance( "Post" ).orderBy( "author.team.name" );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();UserPermission.permission()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 |
+----------------+---------------------------+----------------+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" );
}
}