Answer the question
In order to leave comments, you need to log in
How to properly pack a hierarchically merged construction in Yii2 (with or join)?
Good afternoon, there is such a request:
(get products related to the category and its descendants)
$products = Products::findBySql("
SELECT
*
FROM
m_products
WHERE
category_id
IN (
SELECT
id
FROM
t_products
WHERE
lft >= '{$model->lft}'
AND
rgt <= '{$model->rgt}'
)
"
)->andWhere([
'exist'=>PublicActiveRecord::EXIST_PUBLIC
])->all();
Answer the question
In order to leave comments, you need to log in
$categories = Category::find()
->where(['>=', 'lft', $model->lft])
->andWhere(['<=', 'rgt', $model->rgt])
->with('products')
->all();
public function getProducts()
{
return $this->hasMany(Product::className(), ['category_id' => 'id']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question