Bookshelf.jsで多対多の関係を作成し、FK列の名前を自分で指定したいと思います。また、次のように、Bookshelfのヘルパーテーブルにもアクセスできるようにしたいと思います。
var Doctor = bookshelf.Model.extend({
patients: function() {
return this.belongsToMany(Patient).through(Appointment);
}
});
var Appointment = bookshelf.Model.extend({
patient: function() {
return this.belongsTo(Patient);
},
doctor: function() {
return this.belongsTo(Doctor);
}
});
var Patient = bookshelf.Model.extend({
doctors: function() {
return this.belongsToMany(Doctor).through(Appointment);
}
});
どうすればいいのですか?
上記の例を考慮に入れると、次のようになります。
var Doctor = bookshelf.Model.extend({
tableName: "d_doctor",
idAttribute: "d_doctorid",
patients: function() {
return this.belongsToMany(Patient).through(Appointment,"a_d_doctorid","a_p_patientid");
}
});
var Appointment = bookshelf.Model.extend({
tableName : "a_appointment",
idAttribute : "a_appointmentid",
patient: function() {
return this.belongsTo(Patient,"a_p_patientid");
},
doctor: function() {
return this.belongsTo(Doctor,"a_d_doctorid");
}
});
var Patient = bookshelf.Model.extend({
tableName : "p_patient",
idAttribute : "p_patientid",
doctors: function() {
return this.belongsToMany(Doctor).through(Appointment,"a_p_patientid", "a_d_doctorid");
}
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加