如何在 Bookshelf.js 中创建与自定义字段名称的多对多关系?
How do I create Many-to-Many relations with custom field names in Bookshelf.js?
我想在 Bookshelf.js 中创建多对多关系,并想自己指定 FK 列的名称。我也希望能够访问 Bookshelf 中的 helper-table,类似于:
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");
}
});
我想在 Bookshelf.js 中创建多对多关系,并想自己指定 FK 列的名称。我也希望能够访问 Bookshelf 中的 helper-table,类似于:
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");
}
});