1

Sails 的填充作为左连接工作。我想做内部连接,

任何人都可以帮助我如何在不编写原始查询的情况下使用填充来编写内部联接。我正在使用 MYSQL 适配器。

4

1 回答 1

1

例如,如果您有两个模型 user ,联系人关系一对多,您尝试编写如下内容:-

第一个模型会像这样:-

用户.js

module.exports = {
  schema: true,
  tableName: 'userdata',
  attributes: {
    anyField: {
      type: 'integer',
      required: false,
      size: 56,
    },
    contacts: {
      collection: 'Contact',
      via: 'user'
    }

};

联系.js:-

module.exports = {

  attributes: {
    name: {
      type: 'string'
    },
        mobile_number: {
      type: 'string'
    },

    user: {
      model: 'User'
    }
};

你可以得到这样的填充结果:-

User.find(user.id).populate('contacts').exec(function (err, user) {
  // this contains  user object  and populated by contacts 
     console.log(user[0].contacts);
});

你会得到如下结果:-

{
name:'tets',
contacts:[{
 'name':'test',
'mobile_number':'00000'
},
{
 'name':'test',
'mobile_number':'00000'
},
{
 'name':'test',
'mobile_number':'00000'
}]
}
于 2016-06-13T22:45:37.547 回答