2017-09-26 137 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 ") 
    }); 

希望使用上面的例子会帮助你。

相关问题