Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
// Post.cfc
component extends="quick.models.BaseEntity" {
function user() {
return belongsTo( "User" );
}
}// User.cfc
component extends="quick.models.BaseEntity" {
function posts() {
return hasMany( "Post" );
}
}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 );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 ] );// User.cfc
component extends="quick.models.BaseEntity" {
function permissions() {
return belongsToMany( "Permission" );
}
}// Permission.cfc
component extends="quick.models.BaseEntity" {
function users() {
return belongsToMany( "User" );
}
}// User.cfc
component extends="quick.models.BaseEntity" {
function profile() {
return hasOne( "UserProfile" );
}
}return hasOne("UserProfile", "FK_userID");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 );return belongsTo("UserProfile", "FK_userID", "profile_id");// UserProfile.cfc
component extends="quick.models.BaseEntity" {
function user() {
return belongsTo( "User" );
}
}// 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" );
}
}var posts = user.getPosts();var newestPosts = user
.posts()
.orderBy( "publishedDate", "desc" )
.get();// Comment.cfc
component extends="quick.models.BaseEntity" {
function post() {
return polymorphicBelongsTo( "commentable" );
}
}// This will only find posts the user has written.
var post = user.posts().findOrFail( rc.id );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();// User.cfc
component extends="quick.models.BaseEntity" {
function permissions() {
return hasManyThrough( "Permission" );
}
}// 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",
intermediate = "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" );
}
}// 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" );
}
}