config()
method of a model that relates to another model within your application.postid
, then we would have this config()
method within our comment model:post / comment
relationship mentioned above for belongsTo()
. If we were to talk to ourselves, we would say, "A post has many comments." And that's how you should construct your post model:comment
and not comments
. No need to worry: Wheels understands the need for the plural in conjunction with the hasMany()
method.user
and profile
. A lot of websites allow you to enter required info such as name and email but also allow you to add optional information such as age, salary, and so on. These can of course be stored in the same table. But given the fact that so much information is optional, it would make sense to have the required info in a users
table and the optional info in a profiles
table. This gives us a hasOne()
relationship between these two models: "A user has one profile."profile
model would look like this:user hasOne() profile
, and a profile belongsTo()
a user
. Generally speaking, all associations should be set up this way. This will give you the fullest API to work with in terms of the methods and arguments that Wheels makes available for you.profile
is dependent on a user
. When you delete the user
, you would usually want to delete the profile
as well.dependent
with one of the following values, and CFWheels will automatically deal with the dependency.hasOne()
and hasMany()
associations, but not belongsTo()
.employees
table and share the same Employee
model.employees
table will be joined to itself using the managerid
column. CFWheels will handle the aliasing of the (otherwise duplicated) table names. It does this by using the pluralized version of the name you gave the association (in other words "managers" in this case).select
argument.joinType
for belongsTo()
is inner
, employees without a manager assigned to them will not be returned in the findAll()
call above. To return all rows you can set jointype
to outer
instead.id
appended to the end. So to link up our table to the employees
table, the foreign key column should be named employeeid
.foreignKey
argument in your models' belongsTo()
calls.author
and post
, but you didn't use the naming convention and instead called the column author_id
. (You just can't seem to let go of the underscores, can you?)config()
method would then need to look like this:authors
table automatically so that you can use that data along with the data from posts
.include
argument. You can for example return data for authors, posts
, and bios
in one call like this:author
on the post
model instead of on the comment
model. (Looking at the comment
model is the default behavior when not using parenthesis.)select
argument in the finder functions.name
in both your posts
and authors
tables, then you could use the select
argument like so:post
has many comments
. What happens then is that Wheels will enrich the post model by adding a bunch of useful methods related to this association.comments
that have been submitted for a post
, you can now call post.comments()
. In the background, Wheels will delegate this to a findAll() call with the where
argument set to postid=#post.id#
.post
has many comments
through a hasMany() call, here are the methods that will be made available to you on the post
model.XXX
below with the name of the associated model (i.e. comments
in the case of the example that we're using here).model("comment").findAll(where="postid=#post.id#")
.model("comment").updateByKey(key=comment.id, postid=post.id)
.NULL
. Similar to calling model("comment").updateByKey(key=comment.id, postid="")
.model("comment").deleteByKey(key=comment.id)
.NULL
. Similar to calling model("comment").updateAll(postid="", where="postid=#post.id#")
.model("comment").deleteAll(where="postid=#post.id#")
.model("comment").count(where="postid=#post.id#")
.model("comment").new(postid=post.id)
.model("comment").create(postid=post.id)
.model("comment").exists(where="postid=#post.id#")
.model("comment").findOne(where="postid=#post.id#")
.author
has one profile
through a hasOne() call, here are the methods that will be made available to you on the author
model.comment
belongs to a post
through a belongsTo() call, here are the methods that will be made available to you on the comment
model.post.comments(order="createdAt DESC")
, and the order
argument will be passed along to findAll()
.author.setProfile(profile)
will perform the same task as author.setProfile(1)
. (Of course, we're assuming that the profile
object in this example has a primary key value of 1
.)author.posts()
call in the scenario above, for example.XXX()
method, perhaps we'd want to limit a post
's comment listing to just ones created today. We can pass a where
argument similar to what is passed to the findAll()
function that powers XXX()
behind the scenes.customers
and publications
. A customer
can be subscribed to many publications
, and publications
can be subscribed to by many customers
. In our database, this relationship is linked together by a third table called subscriptions
(sometimes called a bridge entity by ERD snobs).subscriptions
called customerid
and publicationid
.subscription
bridge entity to get the same effect:shortcut
argument to hasMany(), you can have Wheels create a dynamic method that lets you bypass the join model and instead reference the model on the other end of the many-to-many relationship directly.customer
model to look like this instead:through
argument.through
argument accepts a list of 2 association names. The first argument is the name of the belongsTo()association (set in the subscription
model in this case), and the second argument is the hasMany() association going back the other way (set in the publication
model).