Answer the question
In order to leave comments, you need to log in
How to test this class?
<?php
namespace app\models;
use app\models\relations\Categories;
class MenuSite {
//отправление дерева категорий пользователю
public function getMenu() {
$categoriesList = $this->categoriesList();
$treeCategories = $this->buildingTreeOfCategories($categoriesList);
return $treeCategories;
}
//получение списка категорий посредством SQL запроса
private function categoriesList() {
return $categoriesList = Categories::find()
->indexBy('id')
->asArray()
->all();
}
//формирование дерева с неограниченной вложенностью категорий
private function buildingTreeOfCategories($categoriesList) {
$treeCategories = [];
foreach($categoriesList as $id => &$category) {
if ($category['id_parent'] === '0') {
$treeCategories[$id] = &$category;
}
$categoriesList[$category['id_parent']] ['subcategories'][$category['id']] = &$category;
}
return $treeCategories;
}
}
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