[英]Loopback join/include two collections
I would like to include my product_product model inside product_template. 我想将我的product_product模型包含在product_template中。
1 - Each product template has its own product_product variations "HasMany" . 1-每个产品模板都有自己的product_product变体“ HasMany”。
2 - product_product has only one template "BelongsTo" product_template 2-product_product只有一个模板“ BelongsTo” product_template
3- product_template should be filled with only related product_product variations. 3- product_template应该只填充相关的product_product变体。
4- The two models are saved seprately, so when I call for find()
function I would like to get a product_template model filled with the product_product related to it (Could be more than one) 4-这两个模型分别保存,因此当我调用find()
函数时,我想获得一个product_template模型,其中填充了与之相关的product_product(可以多个)
Get product template function : 获取产品模板功能:
Producttemplate.find({
include: {
relation: 'variations',
scope: {
fields: ['sku', 'name', 'price', 'regular_price', 'weight', 'description', 'stock_quantity'],
},
},
})
product_product Model : product_product型号:
This model should be included in the product_template 此模型应包含在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 Model : product_template型号:
This model should include the product_product 该模型应包含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": {}
}
Result : 结果:
This the result of get product template above : 这是上面获取产品模板的结果:
{ 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 [] }
When I point into variations i get a function and into variations.list i get undefined any ideas how to get exact structure ? 当我指向变量时,我得到一个函数,并指向variances.list,但我不确定如何获得确切结构的任何想法?
example code part of my model "TeamRole" which belongsTo "Team" and User" in model level. 我的模型“ TeamRole”的示例代码部分,在模型级别属于“ Team”和“用户”。
teamRole.json teamRole.json
"validations": [],
"relations": {
"team": {
"type": "belongsTo",
"model": "Team",
"foreignKey": ""
},
"user": {
"type": "belongsTo",
"model": "User",
"foreignKey": ""
}
}
Example search query for team role, 团队角色的搜索查询示例,
query.js 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 ")
});
Hope using above example will help you. 希望使用上面的示例对您有所帮助。
sorry for late response, it was just misunderstanding of the relation and structure, i made a function that verifies if the product model exists in the template if yes, i push the template id in the product model and it worked fine. 对不起,我很晚才回答,这只是对关系和结构的误解,我做了一个函数来验证产品模型是否存在于模板中(如果是),我将模板ID推入产品模型中,并且工作正常。
product_product Model : I deleted the relation in the model below because it's useless, so i removed id property in sku and removed _id_Odoo field, then i added id field and parent_template field that contains the template model id. product_product模型:我删除了下面模型中的关系,因为它没有用,所以我删除了sku中的id属性,并删除了_id_Odoo字段,然后添加了包含模板模型ID的id字段和parent_template字段。
{
"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 Model : I removed the _id_odoo and id property in sku and created an id for this model and in relation i made parent_template as foreignkey 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"
}
},
#############
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.