如何按多对多的关系实体ID查找实体

时间:2020-09-29 11:01:36

标签: node.js typeorm

我有许多链接的标签和文章实体。 如果我知道商品编号,如何找到标签?

@Entity("articles")
class ArticleEntity {
  @ManyToMany(() => TagEntity, tag => tag.articles)
  @JoinTable()
  tags: TagEntity[];
}

@Entity("tags")
class TagEntity {
  @ManyToMany(() => ArticleEntity, article => article.tags)
  articles: ArticleEntity[];
}

const articleId = 1;
this.tagRepo.find({ where: { articles: { id: articleId } } }); // ??

1 个答案:

答案 0 :(得分:0)

  1. 要加载带有标签的文章,您必须在 paid week total invoice 1 2 3 4 5 6 7 8 week 1 800k 10% 5% 20% week 2 600k 11% 20% week 3 1m 15% 中指定关联性
FindOptions
  1. 使用const articleId = 1; const article = await connection.getRepository(ArticleEntity) .findOne(articleId, {relations: ["tags"]}); console.log(article.tags); // contains all tags related to the article.id = 1 可以加入他们
QueryBuilder