D
D
dkrylov2017-05-04 23:09:43
Yii
dkrylov, 2017-05-04 23:09:43

How to display subcategories in breadcrumbs in yii2?

Hello, dear yiigrammers)
Please tell me how to implement breadcrumbs for nested categories.
Those. there is a table:
id | parent_id | alias | title Categories
in it:
Category №1->Subcategory of category №1 -> Subcategory of subcategory of category №1
(/category_1) -> (/category_1/subcategory) -> (category_1/subcategory/sub_subcategory)
I get data about the current category in the following way:

public function actionView($alias)
{
    $alias = end(explode('/', $alias));

    $catalog_row = Catalog::find()->where(['alias' => $alias])->one();
}

And I need to display bread crumbs in the same form. While very bad thoughts come into my head - to sort through all the alias elements and pull data from the database. But I believe in the best that there is a normal way.
I googled it but couldn't find it, please help me. Or send)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan, 2017-05-05
@LiguidCool

The implementation of a tree structure is one of the most hemorrhoidal and often encountered tasks in the web.
https://gist.github.com/codedokode/10539720
Naturally, querying the database in a loop is the bottom ... One of the most elegant methods of course is Nested Tree, which allows almost everything to be implemented with one SQL. And here is a good example, you can copy requests (just substitute your data).
Well, for yii2, Google issued https://habrahabr.ru/post/266155/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question