我尝试通过创建belongsTo
关系在Loopback中设置基本模型关系。
我有两个定义如下的模型:
Contract.json
{
"name": "Contract",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"mssql": {
"schema": "dbo",
"table": "Contract"
},
"properties": {
"contractid": {
// some property stuff
},
"validations": [],
"relations": {
"employee": {
"type": "belongsTo",
"model": "Employee",
"foreignKey": ""
}
},
"acls": [],
"methods": []
}
Employee.json
{
"name": "Employee",
"base": "PersistedModel",
"idInjection": false,
"options": {
"validateUpsert": true
},
"mssql": {
"schema": "dbo",
"table": "Employee"
},
"properties": {
"employeeid": {
// some property definitions...
}
},
"validations": [],
"relations": {
"contracts": {
"type": "hasMany",
"model": "Contract",
"foreignKey": ""
}
},
"acls": [],
"methods": []
}
当我使用GET
Contract
对id
发出/api/Contracts/55
请求时,我会得到结果:
致电:{
"contractid": 55,
"employeeId": 83
}
GET
到目前为止哪个好。但是当我在Contract
上发出Employee
请求时,我也希望得到/api/Contracts/55/employee
这样的输出:
致电:{
"contractid": 55,
"employee": {
"employeeid": 83
}
}
Employee
但我只是在没有Contract
的情况下获取{
"employeeid": 83
}
个对象:
textField.inputAccessoryView = toolbar;
textField.inputView = datepicker;
为什么?
我做错了吗?或者我的期望是否错误?
答案 0 :(得分:2)
您可以执行/api/Contracts/55?filter[include]=employee
或:
/api/Contracts/55?filter={"include":"employee"}
您可以通过添加“范围”部分来设置合同模型默认范围以包括员工对象:
"scope": {
"include": "employee"
}