gpt4 book ai didi

typeorm - 如何使用 typeorm 加载与树实体的关系?

转载 作者:行者123 更新时间:2023-12-05 06:25:24 41 4
gpt4 key购买 nike

我有一个 Role Entity 和一个 Route Entity,它们是树形结构,它们是 ManyToMany 关系。

现在我想通过 RoleRepository.find({relations: ['routes']}) 检索所有角色,这将按预期加载所有角色数据,但是 routes 属性不会自动加载它的子数据,看起来像:

[{
id: 1,
name: 'route1',
routes: [{
id: 1,
path: '/home'
}]
}]

我已经检查了所有文档,但没有任何线索。

@Entity()
export class Role {
@PrimaryGeneratedColumn()
public id: number

... some other columns

@ManyToMany(type => Route, route => route.roles)
public routes: Route[]
}
@Entity()
@Tree('materialized-path')
export class Route {
@PrimaryGeneratedColumn()
public id: number

@TreeParent()
public parent: Route

@TreeChildren({ cascade: true })
public children: Route[]

@ManyToMany(type => Role, role => role.routes)
@JoinTable()
public roles: Role[]
}

最佳答案

我认为在树实体创建时获取关系的正确方法是:

await getManager().getTreeRepository(Route).findTrees({ relations: ["roles"] });

关于typeorm - 如何使用 typeorm 加载与树实体的关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57104735/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com