在belongsTo关系中获取完整的json结构

时间:2015-06-19 07:28:38

标签: loopbackjs strongloop

我尝试通过创建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 Contractid发出/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;

为什么?

我做错了吗?或者我的期望是否错误?

1 个答案:

答案 0 :(得分:2)

您可以执行/api/Contracts/55?filter[include]=employee

或:

/api/Contracts/55?filter={"include":"employee"}

您可以通过添加“范围”部分来设置合同模型默认范围以包括员工对象:

"scope": {
    "include": "employee"
  }