A
A
Alexey Chertok2018-02-21 16:40:11
Yii
Alexey Chertok, 2018-02-21 16:40:11

How to get category parent title in Yii2?

Here is the request .. Tell me how to get the title parent_id and not id.

public function getCategoriesValues()
    {
        $query = $this->getQuery(['applyCategory_id']);

        $query->select(['{{%categories}}.id', '{{%categories}}.parent_id', '{{%categories}}.title']);
        $query->leftJoin('{{%categories}}', '{{%products}}.category_id = {{%categories}}.id');
        $query->groupBy('{{%categories}}.title');
        $query->orderBy('{{%categories}}.title');

        return $query->all();
    }

Thanks in advance !

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2018-02-21
@BarnyBroken

Good afternoon.
I'll add to my previous answer.
Create table aliases '{{%categories}} as main_categories' and '{{$categories}} as sub_categories'.
And then refer to the table by these aliases.

$query->select(['main_categories.title', 'sub_categories.title']);

M
Maxim Fedorov, 2018-02-21
@qonand

To get the title of the parent category, you first need to get the category entry itself. This can be done with another join with a category table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question