Answer the question
In order to leave comments, you need to log in
How in Yii2 to get the ownership of a Product from the parent of the category it refers to?
Good afternoon,
There are 2 tables -
products
.id
.name
.categoryid
category (nested set)
.id
.name
.lft
.rgt
.depth
.parent_id
There is a relationship in the Products model :
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
$model = Products::findPublished()->andWhere([
'category_id'=>$cid
]);
Answer the question
In order to leave comments, you need to log in
1. Get the values of the left and right keys of the selected category using the query:
SELECT
lft,
rgt
FROM
category
WHERE
id = <id>
SELECT
*
FROM
products
WHERE
category_id IN (
SELECT
id
FROM
category
WHERE
lft >= <left_key>
AND rgt <= <right_key>
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question