首页 文章

环回:将模型关系保存到数据库

提问于
浏览
0

我一直在阅读Loopback中的模型关系,但我不确定在持久化数据模型时如何保存关系 . 让我使用以下示例 .

两种型号: CategoryArticle

A Category has many Articles, which uses a foreign key categoryId

假设有一个网页有一个下拉类别列表,列表中每个类别的值都是数据库中自动递增的 Id . 在下拉列表下方,有一个输入字段和一个文本框,供您输入文章 Headers 和文本正文 . 现在我的问题是:

当您点击保存按钮时, Article model 以及所选的 category Id 到POST API的文章 . 我想知道如何保存一对多的关系 . 您是否检索类别Id的类别对象并使用aricle对象保存它,或者您只是在 Article 数据库表中保存带有类别Id的文章对象?

我有Hibernate的背景知识,但不知道如何在loopback中正确使用关系 . 回复非常感谢,一个例子会很棒 .

Edit :这不是我的问题的直接答案,但我发现这个github repo非常有助于实现我所寻找的东西,并且它提供了将angularJS和loopback集成为后端的良好示例 . 它在这里:https://github.com/beeman/loopback-angular-admin

1 回答

  • 1

    我想在google群组中发布我从 Líus Fontenelle Carneiro 获得的回答,以防万一将来有人提出同样的问题 .

    Assuming you have Category hasMany Articles, Category endpoint will receive
    by default a URL to handle the relation CRUD operations (e.g.
    /categories/id/articles). When performing a POST to this URL, you're giving the
    id that loopback will look for at the first place. If found, its id 
    automatically fill related foreign key on Article nested model, together with
    other fields you sent to fill Article properties.
    

相关问题