Creation of category trees



  • Someone's gonna find the job sucked, but I didn't find the solution, so please help me than you can.

    So there's a standard table category(id, title, parent_id). Parent_id is the external key to the reference to the id parent of the same table.

    Getting all categories out of the base with one request:

    $categories = Category::find()->asArray()->all();
    

    $categories are presented as follows:

    [
        ['id'=>0, 'title'=>'Электроника', 'parent_id' => null],
        ['id'=>1, 'title'=>'Компьютеры', 'parent_id' => 0],
        ['id'=>2, 'title'=>'ПК', 'parent_id' => 1],
        ['id'=>3, 'title'=>'Ноутбуки', 'parent_id' => 1],
    
    ['id'=>4, 'title'=>'Мобильные телефоны', 'parent_id' => 0],
    
    
    ['id'=>5, 'title'=>'Бытовая химия', 'parent_id' => null],
    ['id'=>6, 'title'=>'Порошок', 'parent_id' => 5],
    ['id'=>7, 'title'=>'Мыло', 'parent_id' => 5],
    

    ]

    etc., infinity category (unknown)

    It is necessary to make a multi-dimensional large-scale tree in which the subsidiaries are placed in parenthood (for an eventual timber withdrawal in submissions). How do you do that? What options? Thank you.



  • Look towards NestedSetsBehavior. https://github.com/creocoder/yii2-nested-sets If you use Yii2.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2