Yii
- 65 ответов
- 0 вопросов
18
Вклад в тег
class Menu extends ActiveRecord
{
// ...
public static function getList()
{
$data = static::find()
->select(['id', 'parent_id', 'title'])
->orderBy('parent_id ASC')
->asArray()
->all();
$sort = new SortList([
'data' => $data,
'prefix' => '------',
]);
$sortList = ArrayHelper::map($sort->getList(), 'id', 'title');
return $sortList;
}
}
class SortList extends Object
{
public $data;
public $prefix = ' ';
protected function getPath($category_id, $prefix = false)
{
foreach ($this->data as $item) {
if ($category_id == $item['id']) {
$prefix = $prefix ? $this->prefix . $prefix : $item['title'];
if ($item['parent_id']) {
return $this->getPath($item['parent_id'], $prefix);
} else {
return $prefix;
}
}
}
return '';
}
public function getList($parent_id = 0)
{
$data = [];
foreach ($this->data as $item) {
if ($parent_id == $item['parent_id']) {
$data[] = [
'id' => $item['id'],
'title' => $this->getPath($item['id'])
];
$data = array_merge($data, $this->getList($item['id']));
}
}
return $data;
}
}