第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Yii2 按屬性過(guò)濾器過(guò)濾產(chǎn)品

Yii2 按屬性過(guò)濾器過(guò)濾產(chǎn)品

PHP
一只萌萌小番薯 2022-01-14 17:11:18
我有一個(gè)我想按屬性過(guò)濾的產(chǎn)品數(shù)據(jù)庫(kù)。我有一段產(chǎn)品。每個(gè)產(chǎn)品都應(yīng)該有一組屬性,例如顏色和重量。我有 3 個(gè)產(chǎn)品。第一個(gè)產(chǎn)品具有屬性:顏色=紅色,第二個(gè)產(chǎn)品具有顏色=紅色和重量=1000kg,最后一個(gè)產(chǎn)品具有顏色=黑色;當(dāng)我選擇紅色和黑色時(shí),一切正常,但是當(dāng)我選擇紅色和重量 1000 公斤的過(guò)濾器時(shí),沒(méi)有顯示產(chǎn)品。這是我ProductSearch模型的一部分。public function search($cat, $params=[]){    $query = Product::find();    $query->joinWith(['productDesc','productCategory','productAttributes']);    $query->andFilterWhere([        'product.id'                => $this->id,        'product.quantity'          => $this->quantity,        'product.stock_status_id'   => $this->stock_status_id,        'product.product_status_id' => $this->product_status_id    ]); // and many more    // Getting data from url:     // category?categories_path=some_category&f=6-Red;7-1000kg;&sort=price_vat    if(isset($params['f'])) {         $filters_raw = explode(';', $params['f']);        $filters_raw = array_filter($filters_raw);        $attr_ids    = [];        $attr_values = [];        foreach ($filters_raw as $filter_arr) {            $filters = explode('-', $filter_arr);            $filter_results[$filters[0]][] = $filters[1];        }    }    if(isset($filter_results)) {        foreach ($filter_results as $attr_id => $filter_res) {            $query->andFilterWhere([                'and',                 ['product_attribute.attribute_id' => $attr_id],                ['product_attribute.value'        => $filter_res]            ]);        }    }}怎么了?
查看完整描述

1 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果一個(gè)產(chǎn)品可以有多個(gè)屬性,簡(jiǎn)單的連接會(huì)為每個(gè)屬性生成多行(因此一個(gè)產(chǎn)品可能會(huì)重復(fù)多次):


| product.id | product_attribute.attribute_id | product_attribute.value |

| ---------- | ------------------------------ | ----------------------- |

| 1          | 120                            | red                     |

| 1          | 121                            | 1000kg                  |

因此,如果您創(chuàng)建一個(gè)包含兩個(gè)屬性條件的查詢(xún),它們將永遠(yuǎn)不會(huì)被滿(mǎn)足,因?yàn)闆](méi)有任何行會(huì)匹配“顏色 = 紅色和重量 = 1000kg”條件。您需要在一行中加入多個(gè)屬性,以獲得如下內(nèi)容:


| product.id | pa1.attribute_id | pa1.value | pa2.attribute_id | pa2.value |

| ---------- | ---------------- | --------- | ---------------- | --------- |

| 1          | 120              | red       | 121              | 1000kg    |

為此,您需要?jiǎng)h除帶有屬性的直接連接:


$query->joinWith(['productDesc','productCategory']);

并為每個(gè)過(guò)濾器分別連接每個(gè)屬性:


foreach ($filter_results as $attr_id => $filter_res) {

    $query->joinWith("productAttributes as productAttributes{$attr_id}");

    $query->andWhere([

        "productAttributes{$attr_id}.attribute_id" => $attr_id,

        "productAttributes{$attr_id}.value" => $filter_res,

    ]);

}



查看完整回答
反對(duì) 回復(fù) 2022-01-14
  • 1 回答
  • 0 關(guān)注
  • 156 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)