How to join multiple entities on a foreign ID in Symfony 4 using a query builder?

后端 未结 3 975
一整个雨季
一整个雨季 2021-01-21 09:07

I\'m trying to learn Symfony. Today I was following The associations tutorial. I decided to make a small application that a House, Kitchens, Bedrooms, and cabinets. I (tried to

3条回答
  •  野性不改
    2021-01-21 09:33

    In your debug bar in symfony you should probably be seeing some errors in doctrine. These won't show up in PHPStorm. You need to rename $kitchen and $bedroom to their plural forms $kitchens and $bedrooms (and change your getters/setters to match) since this is how you define things in the owning side of your doctrine relationships.

    A simpler approach than your repository method would be to do what you want in your controller to let doctrine do your heavy lifting:

    $cabinets = [];
    
    $house = $bedroom->getHouse();
    $kitchens = $house->getKitchens();
    foreach ($kitchens as $kitchen) {
        $kitchenCabinets = $kitchen->getCabinets();
        $cabinets = array_merge($cabinets, $kitchenCabinets);
    }
    

提交回复
热议问题