繁体   English   中英

回送联接/包括两个集合

[英]Loopback join/include two collections

我想将我的product_product模型包含在product_template中。

1-每个产品模板都有自己的product_product变体“ HasMany”。

2-product_product只有一个模板“ BelongsTo” product_template

3- product_template应该只填充相关的product_product变体。

4-这两个模型分别保存,因此当我调用find()函数时,我想获得一个product_template模型,其中填充了与之相关的product_product(可以多个)

获取产品模板功能:

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

product_product型号:

此模型应包含在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 [] }

当我指向变量时,我得到一个函数,并指向variances.list,但我不确定如何获得确切结构的任何想法?

我的模型“ TeamRole”的示例代码部分,在模型级别属于“ Team”和“用户”。

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 ")
    });

希望使用上面的示例对您有所帮助。

对不起,我很晚才回答,这只是对关系和结构的误解,我做了一个函数来验证产品模型是否存在于模板中(如果是),我将模板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模型:我在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM