Back
hasMany()
Sets up a hasMany
association between this model and the specified one.
// Specify that instances of this model has many comments (the table for the associated model, not the current, should have the foreign key set on it).
hasMany("comments");
// Specify that this model (let's call it `reader` in this case) has many subscriptions and setup a shortcut to the `publication` model (useful when dealing with many-to-many relationships).
hasMany(name="subscriptions", shortcut="publications");
// Automatically delete all associated `comments` whenever this object is deleted.
hasMany(name="comments", dependent="deleteAll");
// When not following CFWheels naming conventions for associations, it can get complex to define how a `shortcut` works.
// In this example, we are naming our `shortcut` differently than the actual model's name.
// In the models/Customer.cfc `config()` method.
hasMany(name="subscriptions", shortcut="magazines", through="publication,subscriptions");
// In the models/Subscription.cfc `config()` method.
belongsTo("customer");
belongsTo("publication");
// In the models/Publication `config()` method.
hasMany("subscriptions");
Copied!