简体   繁体   中英

Loopback join/include two collections

I would like to include my product_product model inside product_template.

1 - Each product template has its own product_product variations "HasMany" .

2 - product_product has only one template "BelongsTo" product_template

3- product_template should be filled with only related product_product variations.

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)

Get product template function :

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

product_product Model :

This model should be included in the 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 :

This model should include the 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 ?

example code part of my model "TeamRole" which belongsTo "Team" and User" in model level.

teamRole.json

"validations": [],
  "relations": { 
    "team": {
      "type": "belongsTo",
      "model": "Team",
      "foreignKey": ""
    },
    "user": {
      "type": "belongsTo",
      "model": "User",
      "foreignKey": ""
    }
  }

Example search query for team role,

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.

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.

{
      "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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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