2015-07-11 49 views
0

我想在Bookshelf.js中創建一個多對多關係,並且想自己指定FK-columns的名稱。我也希望能夠訪問Bookshelf中的幫助表,以及類似於此:如何在Bookshelf.js中使用自定義字段名稱創建多對多關係?

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); 
    } 

}); 

我該怎麼做?

回答

0

以上面的例子中考慮進去,一會做這種方式:

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"); 
    } 

}); 
相關問題