Loopback join/include 两个集合

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 [] }

当我指向变体时,我得到一个 函数 并指向 variations.list 我得到 undefined 任何关于如何获得精确的想法结构 ?

我的模型 "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 Model : 我删除了下面模型中的关系,因为它没用,所以我删除了 id 属性 in sku 并删除了 _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 和关系 i将 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"
    }
  },
#############
}