Back
hasMany()
1. 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");
2. 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");
3. Automatically delete all associated `comments` whenever this object is deleted.
hasMany(name="comments", dependent="deleteAll");
// When not following Wheels 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.
4. In the app/models/Customer.cfc `config()` method.
hasMany(name="subscriptions", shortcut="magazines", through="publication,subscriptions");
// In the app/models/Subscription.cfc `config()` method.
belongsTo("customer");
belongsTo("publication");
// In the app/models/Publication `config()` method.
hasMany("subscriptions");
Copied!