2016-08-18 1 views
0

私はダイナミックフィールド(仕様)とフィルタを持つオンラインストアを持っています。すべての仕様は表に格納されています。例:Yii2は動的にjoinWithを追加します

product_specifications: 
-id 
-product_id 
-spec_id 
-spec_val_id 

フィルタは仕様と接続されていますので、フィルタ値を送信すると仕様値が送信されます。たとえば、

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

私はフィルタをループするとき、私は製品のクエリに左のジョインを追加したいと思います。

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

が、私は5つのフィルタを持っている場合、私は5がテーブルproduct_specificationsに参加する必要があります。私はそのようなActiveDataProviderにクエリを持っている

$joinString = ""; 
foreach($filters){ 
$joinString .= "LEFT JOIN product_specifications AS prod_'.filter.' ON ....."; 
} 

ような何か すべての結合を持つ配列をjoinWithで追加したり、クエリチェーンに5つの結合を追加できますか? 1つのカテゴリには、私のフィルタも動的になっているので、ページに5または10個のフィルタがあり、静的な数のjoinWith( 'specs')を追加できません。

ありがとうございます。

私は別の欲望にも同意します。

EDIT:

$dataProvider = new ActiveDataProvider([ 
       'query' => $prodQuery, 
       'pagination' => [ 
        'totalCount' => count($prodQuery->all()), 
        'pageSize' => $pageSize, 
        'route' => Yii::$app->getRequest()->getQueryParam('first_step'), 
       ], 
       'sort' => [ 
        'defaultOrder' => $orderArr, 
       ], 
      ]); 

今私の問題は、ページネーションとソートです:

私はこの

$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のようfindBySqlでクエリを変更します。私のテストサイトでは、私は4つの製品を持っています。私は$ pageSize = 1を設定します;私はページネーションで4ページありますが、すべてのページに4つの製品があります。

私の間違いはどこですか?

答えて

0

は(値はcodntionがそうでないとどこに追加された追加されたヌルなしの場合)あなたは私の問題は、WHERE条件ではありません

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


$prodQuery->andFilterWhere(['attribute1' => $value1]) 
      ->andFilterWhere(['attribute2' => $value2]) 
      ->andFilterWhere(['attribute3' => $value3]) 
      ->andFilterWhere(['attribute4' => $value4]) 
      ->andFilterWhere(['attribute5' => $value5]) 
+0

をandFilterWhereを使用することができるだろう。私のvar $ whereStringにあります。フィルタの条件が$ whereStringで連結されているループフィルタでは、すべての結合を連結できません。例えば、私は2つのフィルタを持っています。私はLEFT JOIN product_specのように、LEFT JOIN product_specをONにする必要があります。LEFT JOIN product_spec prod_spec_2として、私の場所で私はprod_spec_1.spec_id = filterValue1とprod_spec_2.spec_id = filterValue2 – pLe0mAx

関連する問題