2016-03-31 129 views
0

假設通過使用StrongLoop醫生和patiens的標準的例子(https://docs.strongloop.com/display/public/LB/HasManyThrough+relations):Loopback如何查詢基於「hasManyThrough」關係的相關模型?

公共/模型/ physician.json

{ 
    "name": "Physician", 
    "base": "PersistedModel", 
    "properties": { 
    "name": { 
     "type": "string" 
    } 
    }, 
    "validations": [], 
    "relations": { 
    "patients": { 
     "type": "hasMany", 
     "model": "Patient", 
     "foreignKey": "patientId", 
     "through": "Appointment" 
    }, 

公共/模型/ patient.json

{ 
    "name": "Patient", 
    "base": "PersistedModel", 
    "properties": { 
    "name": { 
     "type": "string" 
    } 
    }, 
    "validations": [], 
    "relations": { 
    "physicans": { 
     "type": "hasMany", 
     "model": "Physician", 
     "foreignKey": "physicianId", 
     "through": "Appointment" 
    }, 

共同/models/appointment.json

{ 
    "name": "Appointment", 
    "base": "PersistedModel", 
    "properties": { 
    "appointmentDate": { 
     "type": "date" 
    } 
    }, 
    "validations": [], 
    "relations": { 
    "physician": { 
     "type": "belongsTo", 
     "model": "Physician", 
     "foreignKey": "physicianId" 
    }, 
    "patient": { 
     "type": "belongsTo", 
     "model": "Patient", 
     "foreignKey": "patientId" 
    }, 

讓supouse比一位醫生可以有0,1,2或更多患者。 這樣:

PhysicianId | PatientId 
1----------------1 
1----------------2 
2----------------3 
2----------------4 
3----------------3 
3----------------5 

我怎樣才能讓所有的醫生比擁有相同的病人?

例如:

獲取所有的醫生比有patientId 3的患者?

在這種情況下其結果必然是:PhysicianId 2和3

回答