0
我有一个网上商店与动态字段(规格)和过滤器。所有规格都存储在表格中,例如Yii2动态添加joinWith
product_specifications:
-id
-product_id
-spec_id
-spec_val_id
过滤器与规格连接,所以当我发送过滤器值时,我发送规格值。例如
$_GET['Filters']['filter_id']['spec_val_id']
当我环路滤波器,我想补充左侧加入到产品查询。这样
$joinString = "";
foreach($filters){
$joinString .= "LEFT JOIN product_specifications AS prod_'.filter.' ON .....";
}
事我必须查询ActiveDataProvider这样的:
$prodQuery = Product::find()->joinWith('translation')->joinWith('cats')->[HERE FILTERS JOINS]->where($whereString)->groupBy(['id']);
但是,如果我有5个过滤器,我需要5加入到表product_specifications。 我可以在joinWith中添加一个包含所有连接的数组,或者向查询链添加5个连接? 对于一个类别,我的过滤器也是动态的,所以页面可以有5或10个过滤器,我不能添加静态数量的joinWith('specs')。
在此先感谢。
我也同意不同的desicion。
编辑:
更改查询与findBySql这样
$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` ');
我的数据提供程序:
$dataProvider = new ActiveDataProvider([
'query' => $prodQuery,
'pagination' => [
'totalCount' => count($prodQuery->all()),
'pageSize' => $pageSize,
'route' => Yii::$app->getRequest()->getQueryParam('first_step'),
],
'sort' => [
'defaultOrder' => $orderArr,
],
]);
现在我的问题是分页和排序。在我的测试网站,我有4个产品,当我设置$ pageSize = 1时,我有4页在分页,但在每一页我有所有4个产品。
我的错误在哪里?
我的问题是不是哪里的条件。它在我的var $ whereString中。当我循环过滤条件的过滤器在$ whereString连接,但我不能连接所有连接。例如,我有2个过滤器,我需要2个左连接,例如,左加入product_spec作为prod_spec_1 ON ....左加入product_spec作为prod_spec_2,所以在我的where条件中我有prod_spec_1.spec_id = filterValue1 AND prod_spec_2.spec_id = filterValue2 – pLe0mAx