2017-09-26 140 views
0

我想在product_template中包含我的product_product模型。Loopback Model Relation:如何在另一個集合中包含集合

1 - 每個產品模板都有自己的product_product變體「HasMany」。

2 - product_product只有一個模板 「屬於關聯」 product_template

3- product_template應該充滿只涉及product_product變化。

4-兩種模型seprately保存,所以當我呼籲find()功能我想得充滿了與之相關的product_product一個product_template模型(可能不止一個)

獲取產品模板功能:

Producttemplate.find({ 
     include: { 
     relation: 'variations', 
     scope: { 
      fields: ['sku', 'name', 'price', 'regular_price', 'weight', 'description', 'stock_quantity'], 
     }, 
     }, 
    }) 

product_product型號:

這個模型應該包括我n個product_template

{ 
     "name": "product_product", 
     "base": "PersistedModel", 
     "strict": true, 
     "options": { 
     "validateUpsert": true 
      }, 
     "properties": { 
     "_id_Odoo": { 
      "type": "number" 
     }, 
     "sku": { 
      "type": "string", 
      "id": true, 
      "required": true, 
      "description": "Yes it's SKU" 
     }, 
     #fields 
     }, 
     "validations": [], 
     "relations": { 
     "product": { 
      "type": "belongsTo", 
      "model": "product_template", 
      "foreignKey": "_id_Odoo" 
     } 
     }, 
     "acls": [], 
     "methods": {} 
    } 

product_template型號:

這個模型應該包括product_product

{ 
    "name": "product_template", 
    "base": "PersistedModel", 
     "strict": true, 
     "options": { 
     "validateUpsert": true 
      }, 
    "properties": { 
    "_id_Odoo": { 
     "type": [ 
     "number" 
     ] 
    } 
    "sku": { 
     "type": "string", 
     "id": true, 
     "required": true, 
     "description": "Yes it's SKU" 
    }, 
    "name": { 
     "type": "string" 
    } 
    }, 
    "scope": { 
    "include": "variations" 
    }, 
    "hidden": ["_id_Odoo"], 
    "validations": [], 
    "relations": { 
    "variations": { 
     "type": "hasMany", 
     "model": "product_product", 
     "foreignKey": "_id_Odoo" 
    } 
    }, 
    "acls": [], 
    "methods": {} 
} 

結果:

這上面獲取產品模板的結果:

{ sku: 'AHWLI05942-FUSCHIA', variations: List [] }, 
    { sku: 'AHWLI05943-BLACK', variations: List [] }, 
    { sku: 'AHWLI05943-BURGUNDY', variations: List [] }, 
    { sku: 'AHWLI05944-BLACK', variations: List [] }, 
    { sku: 'AHWLI05944-MARRON', variations: List [] }, 
    { sku: 'AHWLI05945-BLUE', variations: List [] } 

當我點到的變化,我收到了功能進入variations.list我得到未定義任何想法如何得到確切結構?對於球隊的作用我在模型層次模型 「TeamRole」 這屬於關聯 「團隊」 和用戶」的

+0

能告訴你的背景在其中正在訪問模型?它是通過HTTP還是用於模型的JS? – nVitius

回答

0

對於遲到的迴應感到抱歉,這只是對關係和結構的誤解,我做了一個函數,驗證產品模型是否存在於模板中,如果是的話,我在產品模型中推入模板id,並且工作正常。

product_product型號:我刪除下面的模型之間的關係,因爲它是無用的,所以我在SKU刪除id屬性和刪除_id_Odoo場,然後我說id字段和parent_template場包含模板模型ID。

{ 
     "name": "product_product", 
     "base": "PersistedModel", 
     "strict": true, 
     "options": { 
     "validateUpsert": true 
      }, 
     "properties": { 
     "id": { 
      "type": "number" 
      "id": true 
     } 
     "parent_template": { 
      "type": "number" 
     }, 
     "sku": { 
      "type": "string", 
      "required": true, 
      "description": "Yes it's SKU" 
     }, 
     #fields  
    } 

product_template型號:我刪除了SKU的_id_odoo和id屬性,並創建了一個ID,此模型和關係我做了parent_template爲外鍵

{ 
    "name": "product_template", 
    "base": "PersistedModel", 
     "strict": true, 
     "options": { 
     "validateUpsert": true 
      }, 
    "properties": { 
    "id": { 
     "type": "number", 
     "id" : true 
    } 
    "sku": { 
     "type": "string", 
     "required": true, 
     "description": "Yes it's SKU" 
    }, 
    "name": { 
     "type": "string" 
    } 
    }, 
    "scope": { 
    "include": "variations" 
    }, 

########## 

    "relations": { 
    "variations": { 
     "type": "hasMany", 
     "model": "product_product", 
     "foreignKey": "parent_template" 
    } 
    }, 
############# 
} 
1

示例代碼部分。

teamRole.json

"validations": [], 
    "relations": { 
    "team": { 
     "type": "belongsTo", 
     "model": "Team", 
     "foreignKey": "" 
    }, 
    "user": { 
     "type": "belongsTo", 
     "model": "User", 
     "foreignKey": "" 
    } 
    } 

實例搜索查詢,

query.js

app.models.TeamRole.find({ 
     where: { 
      userId: user.id 
     }, 
     include: { 
     relation: 'team' 
     } 
    },function(err,teams){ 
    console.log("teams will have all the include teams[] with team role ") 
    }); 

希望使用上面的例子會幫助你。

相關問題