【问题标题】:Implementing Class Table Inheritance in Loopback在 Loopback 中实现类表继承
【发布时间】:2016-10-06 17:13:44
【问题描述】:

我正在尝试在 Loopback 模型中实现子类化,其中我有一个包含公共字段的父表和具有特定字段的父表的子表。

一个实际的例子:

Customer

具有适用于个人和组织的字段的模型

  • internal_id
  • created

Person

只有个人特定字段的模型

  • first_name
  • last_name

Organisation

只有组织特定字段的模型

  • registration_number
  • trade_name

所以基本上Person 继承自CustomerOrganisation 也继承自Customer

我已按照Extending Models 的指南创建PersonOrganisation 模型 基于Customer 模型

但是,当我在http://0.0.0:3000/persons 创建Person 时,即通过POST,他是在Person 表中创建的,而不是在Customer 表中创建的。

我假设当我基于另一个模型扩展一个模型时,在保存扩展对象模型时,它也会将公共字段保存到父模型中。

似乎并非如此。如何在 Loopback 中实现这一点?


如果这里需要它们是模型 jsons:

customer.json

{
  "name": "Organisation",
  "plural": "Organisations",
  "base": "Customer",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "registration_number": {
      "type": "string",
      "required": true
    },
    "trade_name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

person.json

{
  "name": "Person",
  "plural": "Persons",
  "base": "Customer",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "first_name": {
      "type": "string",
      "required": true
    },
    "last_name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

organisation.json

{
  "name": "Organisation",
  "plural": "Organisations",
  "base": "Customer",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "registration_number": {
      "type": "string",
      "required": true
    },
    "trade_name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

【问题讨论】:

    标签: javascript inheritance loopbackjs


    【解决方案1】:

    Loopback 不提供这种类型的继承。模型只是模板,扩展模型只是创建一个具有从继承模型中获得的属性/方法的新模板。

    我了解您想要抽象出客户是个人还是组织。这看起来像是 polymorphic relations 的典型用例。多态关系允许您将一个模型与其他几个模型相关联。

    这是我将如何构建应用程序:

    customer.js

    {
      "name": "Customer",
      // ...
      "relations": {
        "customerable": {
          "type": "belongsTo",
          "polymorphic": true
        }
      }, // ...
    }
    

    person.js

    {
      "name": "Person",
      // ...
      "relations": {
        "customer": {
          "type": "hasOne",
          "model": "Customer",
          "polymorphic": {"as": "customerable", "discriminator": "person"}
        }
      }, // ...
    }
    

    organization.js

    {
      "name": "Organization",
      // ...
      "relations": {
        "orders": {
          "type": "hasOne",
          "model": "Customer",
          "polymorphic": {"as": "customerable", "discriminator": "organization"} 
        }
      }, // ...
    }
    

    我使用了this example,因为环回文档不是很清楚

    【讨论】:

    • 谢谢 - 我没有 Order 模型 - 你是说 Customer 吗?
    • 不,我的意思是Orders 存储您的订单和销售。 IMO 你应该放弃客户模型,因为它更多地代表了订单和客户之间的关系。
    • IMO you should drop the customer model since it represents more a relation between an order and a client - 那么PersonOrganisation 之间共有的字段会发生什么情况?在每个模型中复制它们?此外,您拥有的订单模型让我感到困惑 - 我在系统的任何部分都没有也不会有订单
    • 我将多态关系称为orderable,但这不是一个明智的选择。最好的名字实际上是customers,因为这样可以执行GET api/Orders/customers 并获取所有通过命令的人和组织。我将编辑我的答案
    • 你可以复制它们或者建立一个特定的模型来存储它们,然后添加一个关系PersonhasOneCommonPropsOrganisationhasOneCommonPros
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多