/
i-panov
/
php-library-manager
Обзор
Документация
Войти
/
i-panov
/
php-library-manager
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/models/Category.php
56 строк
1 KB
i.panov
view
18 мар 2025, 23:54
18 мар 2025, 23:54
6d4daef
Код
Авторство
О чём код?
<?php namespace app\models; use yii\db\ActiveQuery; use yii\db\ActiveRecord; /** * @property int $id * @property ?int $parent_id * @property string $name * * @property-read ?Category $parent * @property-read Category[] $children * @property-read Book[] $books * @property-read array $breadcrumbs */ class Category extends ActiveRecord { public static function tableName(): string { return 'categories'; } public function getParent(): ActiveQuery { return $this->hasOne(Category::class, ['id' => 'parent_id']); } public function getChildren(): ActiveQuery { return $this->hasMany(Category::class, ['parent_id' => 'id']); } public function getBooks(): ActiveQuery { return $this->hasMany(Book::class, ['category_id' => 'id']); } public function getBreadcrumbs(): array { $category = $this; $breadcrumbs = []; while ($category) { $breadcrumbs[] = [ 'label' => $category->name, 'url' => ['site/category', 'categoryId' => $category->id], ]; $category = $category->parent; } return array_reverse($breadcrumbs); } }