簡體   English   中英

Yii 2 Active Record嵌套了許多關系

[英]Yii 2 Active Record nested many to many relations

我有4個表格產品,屬性,property_items,property_product,與之相關的產品具有許多屬性。 財產有許多property_items。 產品具有許多property_items。 Property_product是中介表。

-- products --
Id   name
1   Arrabiata
2   Bolognese

--properties --
Id   name
1   Pasta type
2   Size

-- property_items --
Id   name                   value      property_id
1    Spinach spaghetti      .....      1
2    Linguine               .....      1
3    Spaghetti              .....      1
4    Small                  ....       2
5    Medium                 ....       2
6    Large                  ...        2

-- property_product  -- 
product_id    property_id  property_item_id
    1             1              1
    1             1              2
    1             2              4
    1             2              6
    2             1              2
    2             1              3
    2             2              5

我嘗試使用Gii創建具有關系的模型。

class Products extends \yii\db\ActiveRecord
{
    //code
    ....

    public function getProperties()
    {
        return $this->hasMany(Properties::className(), ['id' => 'property_id'])
                    ->viaTable('property_product', ['product_id' => 'id']);
    }
}

class Properties extends \yii\db\ActiveRecord
{
  //code
  ...

  public function getPropertyItems()
    {
        return $this->hasMany(PropertyItems::className(), ['property_id' => 'id']);
    }
}

我想查詢具有多重嵌套關系的數據。 如何在一個活動記錄查詢中創建以下格式的json響應?

[
{
    "id": "1",
    "name": "Arrabiata",
    "properties": [
        {
            "id": "1",
            "name": "Pasta type",
            "property_items" : [
                {
                    "id": "1",
                    "name": "Spinach spaghetti",
                },
                {
                    "id": "2",
                    "name": "Linguine",
                },
            ]
        },
        {
            "id": "2",
            "name": "Size",
            "property_items" : [
                {
                    "id": "4",
                    "name": "Small",
                },
                {
                    "id": "6",
                    "name": "Large",
                },
            ]
        },
    ],
},
{
    "id": "2",
    "name": "Bolognese",
    "properties": [
        {
            "id": "1",
            "name": "Pasta type",
            "property_items" : [
                {
                    "id": "2",
                    "name": "Linguine",
                },
                {
                    "id": "3",
                    "name": "Spaghetti",
                },
            ]
        },
        {
            "id": "2",
            "name": "Size",
            "property_items" : [
                {
                    "id": "5",
                    "name": "Medium",
                },
            ]
        },
    ]
}]

我使用此查詢,但花費了太多精力

Menus::find()->with(['products' => function ($query) {
            $query->with(['properties']);
        }])
        ->where(['>', 'status', 0])
        ->andWhere(['resid' => $id])->asArray()->all();;
        foreach ($menus as &$menu) {
            foreach ($menu['products'] as &$product) {
                foreach ($product['properties'] as &$property) {
                    $propertyItems = PropertyItems::find()->where(['property_id' => $property['id']]
                    )
                    ->leftJoin('property_product', 'property_items.id = property_product.property_item_id')
                    ->where(['property_product.product_id' => $product['id'],
                             'property_product.property_id' => $property['id']])
                    ->asArray()->all();
                    $property['propertyItems'] = $propertyItems;
                }
            }
        }

如果我使用此查詢

Menus::find()->with(['products' => function ($query) {
                                        $query->with(['properties' => function ($query) {
                                            $query->with(['propertyItems']);
                                        }
                                        ]);
                                    }
                          ])->asArray()->all();

但結果與我預期的不同,因為所有property_items都會顯示在屬性中,而不是依賴於通過“ property_product”表與產品相關聯

[
{
    "id": "1",
    "name": "Arrabiata",
    "properties": [
        {
            "id": "1",
            "name": "Pasta type",
            "property_items" : [
                {
                    "id": "1",
                    "name": "Spinach spaghetti",
                },
                {
                    "id": "2",
                    "name": "Linguine",
                },
                {
                    "id": "3",
                    "name": "Spaghetti",
                },
            ]
        },
        {
            "id": "2",
            "name": "Size",
            "property_items" : [
                {
                    "id": "4",
                    "name": "Small",
                },
                {
                    "id": "6",
                    "name": "Large",
                },
                {
                    "id": "5",
                    "name": "Medium",
                },
            ]
        },
    ],
},
{
    "id": "2",
    "name": "Bolognese",
    "properties": [
        {
            "id": "1",
            "name": "Pasta type",
            "property_items" : [
                {
                    "id": "1",
                    "name": "Spinach spaghetti",
                },
                {
                    "id": "2",
                    "name": "Linguine",
                },
                {
                    "id": "3",
                    "name": "Spaghetti",
                },
            ]
        },
        {
            "id": "2",
            "name": "Size",
            "property_items" : [
                {
                    "id": "4",
                    "name": "Small",
                },
                {
                    "id": "6",
                    "name": "Large",
                },
                {
                    "id": "5",
                    "name": "Medium",
                },
            ]
        }
    ]
}]

提前致謝。

您必須覆蓋ProductsProperties模型的功能fields()

例如:

class Products extends ActiveRecord {

 function fields(){
    return ['properties'];
   }
 }


class Properties extends ActiveRecord {

     function fields(){
        return ['property_items'];
     }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM