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 uchta jadvalni bog'lash

+2 ovoz
104 marta ko‘rilgan
so‘radi 18 iyul, 17 Farhodjon (1,192 bal)
Assalomu alaykum. Menda 3ta jadval bor:

1) Menu => ['id', 'name'];

2) Category => ['id', 'name', 'menu_id'];

3) Subcategory => ['id', 'name', 'category_id', 'menu_id'];

Men quyidagicha natija olmoqchiman.

menu = [

       'id' => 1,

       'name' => menu1,

        'category' => [

               'id' => 1,

               'name' => category,

               'menu_id' => 1,

               'subcategory' => [

                        'id' => 1,

                        'name' => 'subcat',

                        'menu_id' => 1,

                        'category_id' => 1,

                         ...

                     ]

                  ...

           ]

         ...

]

Men: quyidagicha query qildim:

$menu = Menu::find()->joinWith('category')->joinWith('subcategory')->all();

Ammo natijada subcategory categoryning ichida emas, bir xil etapda chiqyapti. Shu masala boyicha bilganlardan yordam kutib qolaman

3 Javoblar

+1 ovoz
javob berdi 18 iyul, 17 Sardor Dushamov (1,652 bal)
tanlangan 19 iyul, 17 vejon
 
Eng yaxshi javob

Kodni tekshirganim yo'q, lekin tipa shunaqa ko'rinishda bo'ladi, bu oddiy usuli:

$result = array();
foreach($menus as $menu){
    $result['id'] = $menu['id'];
    $result['name'] = $menu['name'];
    $cat = Categorymodel::findAll(['menu_id'=>$menu['id']]);
    if(count($cat)>0){
        foreach($cat  as $c){
            $result['category']['id'] = $c['id'];
            $result['category']['name'] = $c['name'];
            $result['category']['menu_id'] = $c['menu_id'];
            $subcat = Subategorymodel::findAll(['category_id'=>$c['id']]);
            if(count($subcat)>0){
                foreach($subcat as $sub){
                    $result['category']['subcategory']['id'] = $sub['id'];
                    $result['category']['subcategory']['name'] = $sub['name'];
                    $result['category']['subcategory']['menu_id'] = $sub['menu_id'];
                    $result['category']['subcategory']['category_id'] = $sub['category_id'];
                }
            }
        }
    }
    return $result;
}
izoh qoldirdi 18 iyul, 17 Farhodjon (1,192 bal)
Bunda faqat oxirgi elementni qaytaryapti. Sizning algoritmizdan foydalangan holda men biroz o'zgartirish kiritdim:
$result = [];
        $menus = Menu::find()->all();
        for($i = 0; $i < count($menus); $i++){
            $result[$i]['id'] = $menus[$i]['id'];
            $result[$i]['name'] = $menus[$i]['name'];
            $cat = Category::findAll(['menu_id'=>$menus[$i]['id']]);
            for($j = 0; $j < count($cat); $j++){
                $result[$i]['category'][$j]['id'] = $cat[$j]['id'];
                $result[$i]['category'][$j]['name'] = $cat[$j]['name'];
                $result[$i]['category'][$j]['menu_id'] = $cat[$j]['menu_id'];
                $subcat = Subcategory::findAll(['category_id'=>$cat[$j]['id']]);
                for($k = 0; $k < count($subcat); $k++){
                    $result[$i]['category'][$j]['subcategory'][$k]['id'] = $subcat[$k]['id'];
                    $result[$i]['category'][$j]['subcategory'][$k]['name'] = $subcat[$k]['name'];
                    $result[$i]['category'][$j]['subcategory'][$k]['menu_id'] = $subcat[$k]['menu_id'];
                    $result[$i]['category'][$j]['subcategory'][$k]['category_id'] = $subcat[$k]['category_id'];
                }
            }
        }
        return $result;
Javob uchun rahmat
0 ovoz
javob berdi 18 iyul, 17 Clever (169 bal)

3 ta foreach qilib natijani olasiz, join orqali olib bo'lmaydi bunaqa natijani

code:

$menu = Menu::find()->all();

$cat = Category::find()->all();

$subcat = Subcategory::find()->all();

foreach($menu as $m) {

  ...

}

0 ovoz
javob berdi 18 iyul, 17 Nodir (5 bal)

Category modelga

public function getSubCategories()
{
    return $this->hasMany(Subcategory::className(), ['category_id' => 'id']);
}

Menu modelga

public function getCategories()
{
    return $this->hasMany(Category::className(), ['menu_id' => 'id']);
}

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

...