环回;多态的belongsTo和hasManyThrough取决于所有者类型

时间:2016-10-19 15:11:43

标签: relationship loopbackjs

我有一个Bid对象。出价可以属于客户或承运人(多态所属)。

此外,客户可能属于承运人(如果客户属于承运人,他不能出价),并且客户是执行他们竞标的服务的客户。

因此,如果承运人进行投标,他必须为客户分配投标。在这种情况下,许多客户可以有很多出价,但他们不是申请人。

我想知道如何做到这一点的最好方法。这就像一个条件关系。

我在 bid.json

中尝试过此操作
"relations":{
  "bidder":{
    "type": "belongsTo",
    "polymorphic": true
  },
  "customers": {
    "type": "hasMany",
    "model": "customer",
    "foreignKey": "customerId"
  }
}

customer.json

"relations": {
  "bids": {
    "type": "hasMany",
    "model": "Bid",
    "foreignKey": "bidderId",
    "polymoprhic": "bidder"
  }
  "carrier": {
    "type": "belongsTo",
    "model": "Carrier",
    "foreignKey": "carrierId"
  },
  "assignments": {
    "type": "hasMany",
    "model": "Bid",
    "foreignKey": "bidId"
  }
}

carrier.json

"relations": {
  "bids": {
    "type": "hasMany",
    "model": "Bid",
    "foreignKey": "bidderId",
    "polymoprhic": "bidder"
  }
  "carrier": {
    "type": "hasMany",
    "model": "Customer",
    "foreignKey": ""
  }
}

1 个答案:

答案 0 :(得分:0)

我最后添加了一个新实体" Assignment"。这样,如果运营商出价,他可以将客户分配给它。如果客户是投标人,他会自动分配

<强> bid.json

"relations":{
  "bidder":{
    "type": "belongsTo",
    "polymorphic": true
  },
  "assignments": {
    "type": "hasMany",
    "model": "assignment",
    "foreignKey": "bidId"
  } 
}

<强> customer.json

"relations": {
    "bids": {
      "type": "hasMany",
      "model": "Bid",
      "foreignKey": "bidderId",
      "polymoprhic": "bidder"
    }
    "carrier": {
      "type": "belongsTo",
      "model": "Carrier",
      "foreignKey": "carrierId"
    }, 
    "assignments": {
      "type": "hasMany",
      "model": "Assignment",
      "foreignKey": "customerId"
    }
  }

<强> carrier.json

"relations": {
  "bids": {
    "type": "hasMany",
    "model": "Bid",
    "foreignKey": "bidderId",
    "polymoprhic": "bidder"
  },
  "customers": {
    "type": "hasMany",
    "model": "Customer",
    "foreignKey": "carrierId"
  }
}

<强> assignment.json

"relations": {
    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": ""
    },
    "licitacion": {
      "type": "belongsTo",
      "model": "Bid",
      "foreignKey": ""
    }
  }