D
D
Doniyor Mamatkulov2018-03-05 21:28:55
PHP
Doniyor Mamatkulov, 2018-03-05 21:28:55

How to select all nested categories from db?

Hi all! There is an interesting problem, I can not bring it to the end.
1. There is a DB with the doc_structure table. The names and dependencies of departments of the organization are stored there. Here is a screenshot:
5a9d8a66c4ab8183951272.png
2. I wrote a code that pulls out and forms an array of data about departments. Here is the code:

public $data;
public $tree;

$this->data = Structure::find()->asArray()->indexBy('id')->all();

3. There is a function that generates a tree of departments with unlimited nesting . Here is the method:
protected function getTree() {
        $tree = [];
        foreach ($this->data as $id => &$node) {
            if(!$node['parent_id'])
                $tree[$id] = &$node;
            else
                $this->data[$node['parent_id']]['childs'][$node['id']] = &$node;
        }
        return $tree;
    }

Now, attention, task! How and where to pass parameters to get an array of nesting departments only for a specific department? That is, suppose I only want all subordinate departments - the department with ID = 3 (the IT department). In theory, I should get an array of nesting IT Department->Administration Department->SPSS

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question