【问题标题】:How to load tree using Materialized Path tree structure with typeorm如何使用带有 typeorm 的物化路径树结构加载树
【发布时间】:2021-07-23 12:43:10
【问题描述】:

我有一个具有“物化路径”树结构的实体。

我正在尝试加载孩子。它给出了一个空的子对象 下面是实体

 @Entity({ name: 'sector' })
    @Tree('materialized-path')
    export class SectorEntity {
    constructor(data?: Partial<SectorEntity>) {
    Object.assign(this, data);
      }
    @ApiProperty({ type: 'string' })
    @PrimaryGeneratedColumn()
    id: number;

    @ApiProperty()
    @Column()
  name: string;

  @ApiProperty()
  @CreateDateColumn({ type: 'timestamp' })
  createdAt?: Date;

  @ApiProperty()
  @UpdateDateColumn({ type: 'timestamp' })
  updatedAt?: Date;

  @TreeChildren()
  children: SectorEntity[];

  @TreeParent()
  parent: SectorEntity;
}

获取树元素的代码

return typeorm.getTreeRepository(SectorEntity).findTrees();

加载后得到的结果,子对象内没有数据

[
    {
        "id": 1,
        "name": "Abc",
        "createdAt": "2021-04-29T18:50:19.922Z",
        "updatedAt": "2021-04-29T18:50:19.000Z",        
        "children": []
    }
]

请告诉我哪里出错了。

【问题讨论】:

    标签: nestjs typeorm materialized-path-pattern


    【解决方案1】:

    希望您的问题已经得到解决。我也遇到过类似的问题,我在保存新记录时做错了,我只在parent 列中保存了父 ID,但是如果我们查看我们的实体,我们有:parent: SectorEntity; 所以更新我的创建函数以实际为我工作的父实体结尾。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多