select('p.*') ->distinct() ->from(['p' => 'product']) ->orderBy(['p.id' => SORT_DESC]); if ($manufacturerIds !== []) { $query->andWhere(['p.manufacturer_id' => $manufacturerIds]); } if ($categoryIds !== []) { $query->andWhere([ 'exists', (new Query()) ->select('1') ->from(['pc' => 'product_category']) ->where('pc.product_id = p.id') ->andWhere(['pc.category_id' => $categoryIds]), ]); } if ($scaleIds !== []) { $query->andWhere([ 'exists', (new Query()) ->select('1') ->from(['pfv' => 'product_field_value']) ->innerJoin(['f' => 'field'], 'f.id = pfv.field_id') ->where('pfv.product_id = p.id') ->andWhere(['f.code' => 'scale']) ->andWhere(['pfv.value_id' => $scaleIds]), ]); } return $query; } }