-
Notifications
You must be signed in to change notification settings - Fork 372
hasMany
Mousset Axel edited this page Jul 7, 2014
·
5 revisions
Is a many to many relationship (includes join table).
Eg: Patient.hasMany('doctors', Doctor, { why: String }, { reverse: 'patients', key: true }).
Patient can have many different doctors. Each doctor can have many different patients.
This will create a join table patient_doctors when you call Patient.sync():
| column name | type |
|---|---|
| patient_id | Integer |
| doctor_id | Integer |
| why | varchar(255) |
The following functions will be available:
patient.getDoctors(function..) // List of doctors
patient.addDoctors(docs, function...) // Adds entries to join table
patient.setDoctors(docs, function...) // Removes existing entries in join table, adds new ones
patient.hasDoctors(docs, function...) // Checks if patient is associated to specified doctors
patient.removeDoctors(docs, function...) // Removes specified doctors from join table
doctor.getPatients(function..)
etc...To associate a doctor to a patient:
patient.addDoctor(surgeon, {why: "remove appendix"}, function(err) { ... } )which will add {patient_id: 4, doctor_id: 6, why: "remove appendix"} to the join table.
Model.hasMany(
name, // String. Association name
otherModel, // Model. The model we're association to
extraProps, // Object. Extra properties that will appear on the join table
opts // Object. Options for the association
);| option name | type | description |
|---|---|---|
| autoFetch | Boolean | Default: false. If true, association will be automatically fetched with parent. |
| autoFetchLimit | Number | Default: 1. How many levels deep to auto fetch |
| key | Boolean | Default: false (for historical reasons). If true, foreign key columns in the table will form a composite key. |
| mergeTable | String | Custom name for the merge table |
| mergeId | String | Custom name for column referencing this model |
| mergeAssocId | String | Custom name for column referencing other model |
| reverse | String | Default: false. If true, association will be accessible from the other model with the specified name. |
| getAccessor | String | Default: 'get' + Name. Allows overwriting associating accessor. |
| setAccessor | String | Default: 'set' + Name. Allows overwriting associating accessor. |
| hasAccessor | String | Default: 'has' + Name. Allows overwriting associating accessor. |
| delAccessor | String | Default: 'del' + Name. Allows overwriting associating accessor. |
| addAccessor | String | Default: 'add' + Name. Allows overwriting associating accessor. |