Answer the question
In order to leave comments, you need to log in
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:
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();
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;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question