返回包含环回的联接表属性 [英] Return join table attributes in incluce with loopback

查看:69
本文介绍了返回包含环回的联接表属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据结构与回送中描述的数据结构非常相似 HasManyThrough 文档页面.

I've got a data structure fairly similar to the one described on the Loopback HasManyThrough documentation page.

对于给定的医师(例如id 2),我想让他们的所有患者都有约会和约会日期.

For a given Physician (e.g. id 2), I would like to get all their patients with an appointment AND their appointment date.

我可以这样执行GET操作:

GET /physicians/2

带有filter标头{ "include" : {"relation":"patients"} }

我确实得到了医生和患者名单,但是我失去了这种关系.

And I do get the physician, and the list of patients, but I lose the appointmentDate of the relation.

或者,我可以在关系表上执行GET操作,如文档所示:

Or, I can do a GET operation on the relation table like the documentation shows:

GET /appointments

具有filter标头{ "include" : {"relation":"patient"}, "where":{"physicianId":2}} }

我得到了约会,上面有日期和患者,但没有医生的详细信息.

And I get the the appointments, with the date and the patient embedded, but not the physician details.

我似乎无法将两者结合起来.

I can't seem to be able to combine the two.

有没有一种方法可以通过一次查询获取全部数据?

Is there a way to get the whole data with one query?

数据将如下所示:

[
    "name" : "Dr John",
    "appointments" : [ {
        "appointmentDate": "2014-06-01",
        "patient": {
          "name": "Jane Smith",
          "id": 1
        }
    }]
]

我发现的

一种方式技巧是两次定义该关系.一次作为约会表的HasManyThrough和一次HasMany,然后我可以执行以下操作:

One way hack I found is to define the relation twice. Once as a HasManyThrough and once as a HasMany to the appointments table, then I can do something like this:

GET /physicians/2

具有filter标头{ "include" : {"relation":"appointments","scope":{"include":["patient"]} } }

但这似乎不正确,或者可能由于重复的关系导致奇怪的行为..但也许我很偏执.

But that doesn't seem right, or could maybe lead to odd behaviours with the duplicated relation.. but maybe I'm paranoid.

推荐答案

您可以同时包含两个模型

You could include both models

GET /appointments
{ "include": ["patient", "physician"], "where": { "physicianId":2 } }

尽管如此,您将获得大量重复数据(ID为2的医生的详细信息).我相信, HasManyThrough 关系模型最初不应该包含任何其他内容数据,因此有一些限制. 此处是一个相关的github问题.

You will get quite a lot of duplicate data though (details of physician with id 2). I believe, that HasManyThrough relation model was initially not supposed to carry any extra data and therefore, it has some limitations. Here is a related github issue.

这篇关于返回包含环回的联接表属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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