Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.
Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin. Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Yii2 da query builderda muammo

+2 ovoz
43 marta ko‘rilgan
so‘radi 22 iyul, 17 Farhodjon (1,192 bal)
tahrirlandi 23 iyul, 17 vejon
Assalomu alaykum. Men yii2 da bazadan ma'lumotlarni quyidagicha filtrlamoqchi edim. Ya'ni bazadan bitta ma'lumotlar olinayotganda dastlab uni shop_id bo'yicha oladi. Agar uni soni 4 tadan ko'p bo'lsa subcategory_id bo'yicha AND qilib filtr qiladi aks holda  OR bilan filter qiladi. Shundayam soni 4 tadan ko'p bo'lsa category bo'yicha AND qilib filtr qiladi aks holda OR bilan, Yana 4 tadan ko'p bo'lsa menu_id bo'yicha aks holda menu_id bo'yicha oxirida limit(4) ni qo'shib olish kerak. shunda bitta muammo bo'lyapti menda bazadan negadur hammasini olyapti. Kodlarim quyidagicha:

$likeCoupons = Coupons::find()->where(['shop_id' => $coupon->shop_id]);
        if ($likeCoupons->count() > 4)
            $likeCoupons = $likeCoupons->andFilterWhere(['subcategory_id' => $coupon->subcategory_id]);
        else
            $likeCoupons = $likeCoupons->orFilterWhere(['subcategory_id' => $coupon->subcategory_id]);

        if ($likeCoupons->count() > 4)
            $likeCoupons = $likeCoupons->andFilterWhere(['category_id' => $coupon->category_id]);
        else
            $likeCoupons = $likeCoupons->orFilterWhere(['category_id' => $coupon->category_id]);

        if ($likeCoupons->count() > 4)
            $likeCoupons = $likeCoupons->andFilterWhere(['menu_id' => $coupon->menu_id]);
        else
            $likeCoupons = $likeCoupons->orFilterWhere(['menu_id' => $coupon->menu_id]);

        $likeCoupons->limit(4)->all();
izoh qoldirdi 22 iyul, 17 Sardor Dushamov (1,652 bal)
har bir if elsedan keyin countlar sonini echo exit qilib tekshirib ko'rinchi

1 ta javob

0 ovoz
javob berdi 25 iyul, 17 SedBigDaddy (1,237 bal)
tanlangan 28 iyul, 17 Farhodjon
 
Eng yaxshi javob

Siz bir narsani esdan chiqargansiz ActiveRecord bu Decorator Patternda yasalgan.

Shunchun hargal queryni chaqirsangiz sizda u saqlanib qoladi. Masalan $likeCoupons->count() qilsangiz yana bir marta shunday qilolmaysiz. 

shuinchun maslahtim hargal Queryni tenglamasdan clone qilin. masalan 

 $query= Coupons::find()->where(['shop_id' => $coupon->shop_id]);

$count=clone $query;

$cupons=clone $query;
        if ($count->count() < 4)
            $likeCoupons = $cupons->andFilterWhere(['subcategory_id' => $coupon->subcategory_id]);
        else
            $likeCoupons = $cupons->orFilterWhere(['subcategory_id' => $coupon->subcategory_id]);

        if ($count->count() = 4)
            $likeCoupons = $cupons->andFilterWhere(['category_id' => $coupon->category_id]);
        else
            $likeCoupons = $cupons->orFilterWhere(['category_id' => $coupon->category_id]);

        if ($count->count() > 4)
            $likeCoupons = $cupons->andFilterWhere(['menu_id' => $coupon->menu_id]);
        else
            $likeCoupons = $cupons->orFilterWhere(['menu_id' => $coupon->menu_id]);

        $likeCoupons->limit(4)->all();

Assalomu alaykum, yordam.uz saytimizga xush kelibsiz.

Bu saytda o`zingizni qiziqtirgan savollarga javob olishingiz va o`z sohangiz bo`yicha savollarga javob berishingiz mumkin.

Bizning Oilamizga a'zo bo`lganingiz uchun chuqur Minnatdorchilik bildiramiz !!!

Telegram kanal YordamUzRss

...