2017-12-07 4 views
0

typeormを使用し、this exampleに従って階層構造のデータ構造を処理しようとしていますが、階層テーブルを作成してそのパスを読み取る方法についての例ですが、既に子を持つノードがある場合は追加します私はこのような何かを押すことができると信じてより多くの子供たち:typeormを使用して、すでにテーブル閉包を使用して子を持つノードに新しいノードを追加する方法はありますか?

category.children.push(newCategory); 

しかし、それは子供を持っている間カテゴリの子供は空です。

EDIT1:

Here is the code: 

    async function test() { 
    let connection = await createConnection(options); 
    let categoryRepository = connection.getTreeRepository(Category); 
    let cat = await categoryRepository.findOneById(4, { 
     relations: ['children'] 
    }); 
    console.log(JSON.stringify(cat)); 
    let childChildCategory1 = new Category(); 
    childChildCategory1.name = "Child #1 of Child #1 of Category #11111"; 
    childChildCategory1.description = "Child #1 of Child #1 of Category #11111"; 
    cat["__children__"].push(childChildCategory1) 
    await categoryRepository.save(cat); 
    let result = await categoryRepository.findDescendants(cat); 
    console.log(JSON.stringify(result)); 
} 

、ここではその結果である: 最初にconsole.log:

{ 
    "id":4, 
     "name":"Child #2 of Category #1", 
      "description":"Child #2 of Category #1", 
       "level":2, 
        "__children__":[ 
         { "id": 5, "name": "Child #1 of Child #2 of Category #1", "description": "Child #1 of Child #2 of Category #1", "level": 3 }, 
         { "id": 6, "name": "Child #1 of Child #1 of Category #11111", "description": "Child #1 of Child #1 of Category #11111", "level": 3 }, 
         { "id": 7, "name": "Child #1 of Child #1 of Category #11111", "description": "Child #1 of Child #1 of Category #11111", "level": 3 } 
        ], 
         "__has_children__":true 
} 

秒にconsole.log:

[ 
    { "id": 4, "name": "Child #2 of Category #1", "description": "Child 
    #2 of Category #1", "level": 2 }, 
    { "id": 5, "name": "Child #1 of Child #2 of Category #1", 
    "description": "Child #1 of Child #2 of Category #1", "level": 3 }, 
    { "id": 6, "name": "Child #1 of Child #1 of Category #11111", 
    "description": "Child #1 of Child #1 of Category #11111", "level": 
    3 }, 
    { "id": 7, "name": "Child #1 of Child #1 of Category #11111", 
    "description": "Child #1 of Child #1 of Category #11111", "level": 
    3 }, 
    { "id": 8, "name": "Child #1 of Child #1 of Category #11111", 
    "description": "Child #1 of Child #1 of Category #11111", "level": 
    3 } 
] 

答えて

0

あなたが{ cascadeInsert: true, cascadeUpdate: true }を入れました@TreeChildren()デコレータのオプション? categoryをどのように読み込みますか? QueryBuilderで読み込んだ場合、leftJoinAndSelectメソッドを使用して子供に忘れることがあります。

+0

ええ、findOptionsでリレーションシップを追加した後、それは機能しましたが、代わりに 'children'プロパティを返しませんでした。代わりに' --children - ' – user4092086

関連する問題