簡體   English   中英

Sails / WaterLine ORM /具有多次通過:將數據插入聯接表

[英]Sails / WaterLine ORM / Has Many Through: Insert data into join table

我是Sails / WaterLine ORM的新手
我正在關注http://sailsjs.org/documentation/concepts/models-and-orm/associations/through-associations

一個問題。
如何將數據插入到聯接表中?
例如: 用戶m-m寵物

用戶模型

module.exports = {
  attributes: {
    name: {
    type: 'string'
  },
  pets:{
    collection: 'pet',
    via: 'owner',
    through: 'petuser'
  }
}

寵物模型

module.exports = {
  attributes: {
    name: {
    type: 'string'
  },
  color: {
    type: 'string'
  },
  owners:{
    collection: 'user',
    via: 'pet',
    through: 'petuser'
  }
}

PetUser模型(聯接表)

module.exports = {
  attributes: {
    owner:{
      model:'user'
    },
    pet: {
      model: 'pet'
    }
  }
}

可以使用寵物數據(某些記錄包含ID1,ID2,ID3 ...)
我想添加一些寵物的新用戶
PetUser(id,id_of_user,id_of_pet)
1,U1,P1
2,U1,P2

{
    "name" : "John",
    "pets" : [2,3]
}

用戶控制器

module.exports = {

    addUserWithPets: function(req, res) {
    User.create(req.body).exec(function(err, user) {
        if(err){
            throw err;
        }else { 
            /*pets.forEach(function(pet, index){
                user.pets.add(pet);
            })
            user.save(function(err) {});*/

            user.pets.add(data);

            user.save(function(err) {});
        }

        return res.ok({
            data: user
        });

    })
  } 
};

謝謝!

我認為這還沒有實現。

請參考以下問題: 通過 SO 上sails.js中的關聯

這是水線文檔所說的:

“多對多”關聯的行為與“多對多”關聯的行為相同,只是自動為您創建了聯接表。 這使您可以將其他屬性附加到聯接表內部的關系上。

快來了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM