回送3中的标签关系 [英] Tags relationship in loopback 3

查看:58
本文介绍了回送3中的标签关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在环回中如何创建标签? 例如有项目

In Loopback how can I create tags? For example there are projects

{
  id,
  name
}

,并且具有类似模型的标签集合 现在,该项目需要具有多个标签,并且同一标签可以在多个项目中使用.

and there are tags collection with the similar model Now the project needs to have multiple tags, and the same tag can be used in multiple projects.

例如,在创建项目时,用户可以键入已经存在的标签或新标签,然后将其添加到项目中.

For example while creating a project, the user may type already existing tags, or new tags, and those should be added to the project.

在环回框架中找不到我需要的确切关系.你怎么做到的?

I can't find the exact relationship I need in the loopback framework. How do you do that?

推荐答案

TLDR

  1. CREATE TABLE ProjectTag (id AUTO INCREMENT PRIMARY KEY, project_id INTEGER, tag_id INTEGER);

$ lb relation Project has and belongs to many Tag

POST localhost:3000/api/Project

POST http://localhost:3000/api/Projects/{ProjectId}/Tags

第一步创建将Projects链接到Tags的表.

The first step creates the table which links Projects to Tags.

第二个在回送中创建关系并修改您的Project.json relations

The second creates the relation in loopback and modifies your Project.json relations

第三个创建新项目

第四个为该项目创建一个新标签. REST资源管理器中还有很多其他选项.

And the fourth Creates a new tag for that project. There are a bunch more options in the REST explorer.

编辑

要添加已经存在于项目中的标签,请使用PUT /Projects/{PROJECT_ID}/Tags/rel/{TAG_ID}

To add a tag which already exists to a project use PUT /Projects/{PROJECT_ID}/Tags/rel/{TAG_ID}

详细信息

https://loopback.io/doc/en/lb3/HasAndBelongsToMany -relations.html https://loopback.io/doc/en/lb3/HasManyThrough-relations. html

hasAndBelongsToMany本质上与hasManyThrough相同,除了通过隐式创建贯通模型之外.

hasAndBelongsToMany is essentially the same has hasManyThrough, except the through model is implicitly created.

要实现HasManyAndBelongsToMany,必须存在一个表,该表将一个映射到另一个.如果将关系添加到Project模型,则将其称为ProjectTag(我将对其进行定义)有一个回送模型,因为我不知道您的RDBMS).

To implement the HasManyAndBelongsToMany a table must exist which maps one to the other. If you add the relation to the Project model, it will be called ProjectTag (which I'll define has a loopback model because I don't know your RDBMS).

{
  "name": "ProjectTag",
  "properties": {
    "projectId": {
      "type": "Number"
    },
    "tagId": {
      "type": "Number"
    },
    "id": {
      "type": "Number"
    }
  }
}

您可以使用lb relation创建关系.这会将其放入您的模型之一. (我的模型是stackoverflow1)

You can create the relation with lb relation. Which will put this into one of your models. (My model was stackoverflow1)

"stackoverflow2s": {
  "type": "hasAndBelongsToMany",
  "model": "stackoverflow_2",
  "foreignKey": "",
  "options": {
    "nestRemoting": true
  }
}

例如,在创建项目时,用户可以键入已经存在的标签或新标签,然后将其添加到项目中.

For example while creating a project, the user may type already existing tags, or new tags, and those should be added to the project.

您必须首先创建项目POST localhost:3000/api/Project

然后,您可以使用POST http://localhost:3000/api/Projects/{ProjectId}/Tags将标签添加到该特定对象.

Then you can use POST http://localhost:3000/api/Projects/{ProjectId}/Tags To add tags to that specific object.

这篇关于回送3中的标签关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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