Anouar Kacem Anouar Kacem - 1 year ago 131
Node.js Question

Loopback Model Relation : How to include a collection inside another collection

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

product_template should be filled with only related product_product variations that has same _id_Odoo as in product_template

product_product Model :

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

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


CallBack :

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

Answer Source

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"
    }
  },
#############
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download