Dynamically add joinWith

i have an online store with dynamic fields (specifications) and filters. All specifications are stored in table e.g


product_specifications:

-id

-product_id

-spec_id

-spec_val_id

Filters are connected with specifications, so when i send filter value i send specification values. For example


$_GET['Filters']['filter_id']['spec_val_id']

When i loop filters, i want to add left joins to product query. Something like that


$joinString = "";

foreach($filters){

$joinString .= "LEFT JOIN product_specifications AS prod_'.filter.' ON .....";

}

I have query to ActiveDataProvider like that:


$prodQuery = Product::find()->joinWith('translation')->joinWith('cats')->[HERE FILTERS JOINS]->where($whereString)->groupBy(['id']);

But if i have 5 filters, i need 5 joins to table product_specifications. Can i add in joinWith an array with all joins or add 5 joins to query chain? To one category my filters are also dynamically, so the page can have 5 or 10 filters, i can’t add static number of joinWith(‘specs’).

Thanks in advance.

I’m agree with different desicion, too.

Well i change my query with findBySql

Like this:


$prodQuery = Product::findBySql('

              SELECT `product`.* 

              FROM `product` 

              LEFT JOIN `productLang` ON `product`.`id` = `productLang`.`product_id` 

              LEFT JOIN `product_cat` ON `product`.`id` = `product_cat`.`product_id` 

              LEFT JOIN `page` ON `product_cat`.`page_id` = `page`.`id` 

              LEFT JOIN `page` `parent` ON `page`.`id_in` = `parent`.`id`'

              .$joinString.

              ' WHERE ( '

                    .$whereString.

                    ' AND (`language`=\''.$lang->url.'\')) GROUP BY `product`.`id` '); 

And dataprovider:


$dataProvider = new ActiveDataProvider([

                'query' => $prodQuery,

                'pagination' => [

                    'pageSize' => $pageSize,

                    'route' => Yii::$app->getRequest()->getQueryParam('first_step'),

                ],

                'sort' => [

                    'defaultOrder' => $orderArr,

                ],

            ]);

All filters works with pjax, but pagination and order stop working. In my tests i have 4 products, when i seta $pageSize = 1;, i have 4 pages, but in every page i have all 4 products.

Someone can help and tell me where is my error?