【发布时间】:2021-07-28 08:37:55
【问题描述】:
我有一个类别的树形结构。这些类别与其他表有关系。
import {Entity, Column, PrimaryGeneratedColumn, ManyToOne, OneToMany} from "typeorm";
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
@ManyToOne(type => Category, category => category.children)
parent: Category;
@OneToMany(type => Category, category => category.parent)
children: Category[];
@OneToMany(type => Site, site => site.category)
sites: Site[];
}
我获取树的代码:
console.log(await getConnection().getTreeRepository(Category).findTrees());
结果:
[
Category {
pk: 8,
id: '1C606380-020A-4CED-8EBA-AA1C9C16FCF2',
title: 'Category EXPLICIT',
createdBy: 'SAFETYMANUAL',
createdAt: 2021-07-27T19:57:03.213Z,
hidden: false,
orderValue: 0,
deletionDate: null,
childCategories: [ [Category] ]
}
]
我想用关系加载整个树,在本例中是 Sites 数组。将 eager 设置为 true 不会改变任何事情。我只取回没有站点数组的树。是否有可能加入树结构?
【问题讨论】:
-
嘿,我也无法加载树中的关系。你找到解决办法了吗?
-
@rickster 我实现了一个拉取请求,可以加载关系。我为我的问题添加了答案。希望对您有所帮助。
标签: javascript typescript orm typeorm typeorm-activerecord