Answer the question
In order to leave comments, you need to log in
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();
}
Answer the question
In order to leave comments, you need to log in
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']);
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 questionAsk a Question
731 491 924 answers to any question