typeorm查询多托马尼关系
我正在尝试查询桌子产品以查找某些具有“类别“食物”,“冷”的产品。
我希望有人可以给我一个人,以便查询两个或更多类别 我能够使它充满活力,但我在QueryBuilder上挣扎
@Entity()
export class Product{
@Property()
@ManyToMany(() => Category, category=> category.products)
@JoinTable({
name: "product_category",
joinColumn: {
name: "product",
referencedColumnName: "Id"
},
inverseJoinColumn: {
name: "category",
referencedColumnName: "Id"
}
})
categories: Category[];
}
@Entity()
export class Category {
@Property()
@PrimaryGeneratedColumn("uuid")
Id: string;
@Column()
@Property()
name: string;
@Property()
@ManyToMany(() => LibraryItems, libraryItem => libraryItem.categories)
products: Product[];
}
我尝试使用此信息: query.Where(“ categories.name in(:... name1)和categories.name in(:... name2)”,{name1:“ food”,name2:“ cold”})
I'm trying to query over the table products to find, Certain products that have "at least" Category "Food", "Cold".
I hope someone can give me a hand to be able to query for 2 or more Categories
I'm able to make it dynamic but I'm struggling hard on the queryBuilder
@Entity()
export class Product{
@Property()
@ManyToMany(() => Category, category=> category.products)
@JoinTable({
name: "product_category",
joinColumn: {
name: "product",
referencedColumnName: "Id"
},
inverseJoinColumn: {
name: "category",
referencedColumnName: "Id"
}
})
categories: Category[];
}
@Entity()
export class Category {
@Property()
@PrimaryGeneratedColumn("uuid")
Id: string;
@Column()
@Property()
name: string;
@Property()
@ManyToMany(() => LibraryItems, libraryItem => libraryItem.categories)
products: Product[];
}
I have tried using this:query.where("categories.name IN(:...name1) AND categories.name IN(:...name2)", { name1: "Food", name2: "Cold" })
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们讨论。这是查询的一个示例将是:
ps:正如您在评论中提到的那样,这个问题将至少用想要的类别
进行全局示例,其中mysql查询 使用W3School数据库
As we discuss. this is an example of the query will be:
PS: as you mentioned in the comment this question will return the rows with at least the categories wanted
FOR GLOBAL EXAMPLE WITH MYSQL QUERY using W3SCHOOL DATABASE Editor