使用LoopBacks HasManyThrough设置加入模型的自定义属性 [英] Set the custom properties in joining model using LoopBacks HasManyThrough

查看:108
本文介绍了使用LoopBacks HasManyThrough设置加入模型的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在LoopBack 2.1.2中建立多对多关系

I am trying to set up a many to many relationship in LoopBack 2.1.2

http://docs.strongloop.com/display/LB/HasManyThrough+relations

我试图POST/api/Patients/:PatientId/医师来创建一个新的医生,该医生将PatientId链接到新的医生,但是没有在约会模型中设置约会日期.

I tried to POST /api/patients/:patientId/physicians to create a new physician which links the patientId to the new physician, but does not set the appointmentDate in the appointment model.

是否有一个API调用可以在一个事务中创建此API? 向患者添加新医师并设置约会日期的最佳方法是什么? 我必须创建自己的RESTFUL API调用吗?

Is there one API call to create this in one transaction? What is the best way to add a new physician to a patient and setting the appointmentDate? Do I have to create my own RESTFUL API call?

这些是我的json模型

These are my json models

文件名:约会.json

filename: appointment.json

{
    "name": "appointment",
    "base": "PersistedModel",

    "relations": {
        "patient": {
            "type": "belongsTo",
            "model": "patient"
        },
        "physician": {
            "type": "belongsTo",
            "model": "physician"
        }
    },


    "properties": {
        "appointmentDate": {
            "type": "string"
        }
    },

    "validations": [],
    "acls": [],
    "methods": []
}

文件名:Patient.json

filename: patient.json

{
    "name": "patient",
    "base": "PersistedModel",

    "relations": {
        "physicians": {
            "type": "hasMany",
            "model": "physician",
            "through": "appointment"
        }
    },

    "properties": {
        "name": {
            "type": "string"
        }
    },

    "validations": [],
    "acls": [],
    "methods": []
}

文件名:doctor.json

filename: physician.json

{
    "name": "physician",
    "base": "PersistedModel",

    "relations": {
        "patients": {
            "type": "hasMany",
            "model": "patient",
            "through": "appointment"
        }
    },

    "properties": {
        "name": {
            "type": "string"
        }
    },

    "validations": [],
    "acls": [],
    "methods": []
}

推荐答案

免责声明:我是为StrongLoop工作的LoopBack开发人员.

是否有一个API调用可在一事务中创建此API?

Is there one API call to create this in one transaction?

不,目前没有此类API.

No, there is no such API at the moment.

向患者添加新医师并设置约会日期的最佳方法是什么?

What is the best way to add a new physician to a patient and setting the appointmentDate?

您必须发送两个请求:第一个请求创建医师(POST /physicians),第二个请求创建约会(POST /appointments).

You have to send two requests: The first one to create a physician (POST /physicians), the second one to create the appointment (POST /appointments).

或者,您可以使用"Patient hasMany约会"代替"Patient hasMany通过预约的医生",在这种情况下,可以通过

Alternatively, you can use "Patient hasMany appointments" instead of "Patient hasMany physicians through Appointment", in which case the appointment can be added via

POST /patients/:patientId/appointments`

您仍然必须首先创建医师.

You will still have to create the physician first.

我必须创建自己的RESTFUL API调用吗?

Do I have to create my own RESTFUL API call?

您当然可以做到,尽管我个人不理解为什么在这种情况下两个请求是一个问题.对我来说,为给定的病人预约新医生"的操作对我来说很奇怪.对我来说,分两个步骤(创建新医生",再过一段时间预约").

You can certainly do that, although I personally don't understand why two requests are a problem in this case. The operation "create a new physician with an appointment for the given patient" looks weird to me. Two steps ("create a new physician", and some time later "make an appointment") make more sense to me.

但是,如果您有一个很好的示例,可以在一个请求中创建两个记录,那么请在strongloop/loopback中打开github问题以进一步讨论.

However, if you have a good example where it makes sense to create both records in one request, then please open a github issue in strongloop/loopback to discuss this further.

更多信息

目前,已针对"hasAndBelongsToMany"关系调整了"hasMany Through"关系,其中"through"模型只是两个id属性(外键)的容器.这就是为什么POST /api/patients/:patientId/physicians之类的关系方法不支持"appointmentDate"之类的直通"属性的原因.

At the moment, the "hasMany through" relation is tuned for the purpose of "hasAndBelongsToMany" relation, where the "through" model is just a container for the two id properties (foreign keys). That's the reason why the relation methods like POST /api/patients/:patientId/physicians do not support "through" properties like "appointmentDate".

我创建了一个github问题 loopback#466 来讨论如何改进这一部分的LoopBack,请随时在此处发表评论.

I have created a github issue loopback#466 to discuss how to improve this part of LoopBack, feel free to comment there.

loopback-explorer中也存在一个错误(#39 ), UI提示POST /patients/{id}/physicians希望预约,即使实现中希望由Physician代替.

There is also a bug in loopback-explorer (#39), where the UI suggest that POST /patients/{id}/physicians is expecting an Appointment, even though the implementation expects a Physician instead.

这篇关于使用LoopBacks HasManyThrough设置加入模型的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆