Loopback join /包含两个集合

时间:2017-09-26 11:31:36

标签: node.js mongodb loopbackjs

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

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 任何想法如何获得精确结构?

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

对于迟到的回复感到抱歉,这只是对关系和结构的误解,我做了一个函数来验证模板中是否存在产品模型,如果是,我在产品模型中推送模板ID并且工作正常。 / p>

product_product模型:我删除了下面模型中的关系,因为它没用,所以我删除了sku中的id属性并删除了_id_Odoo字段,然后我添加了包含模板模型的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作为foreignkey

{
  "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"
    }
  },
#############
}