2015-09-04 49 views

回答

2

基於您正在使用的數據庫的答案。

例如,您需要在Mongo中填充不加入的值。或者如果您使用的是MySQL或類似軟件,則需要加入表格。

簡而言之,所有這些東西都通過水線覆蓋。所以你可以用關聯在api/models中聲明模型。在Waterline適配器下執行連接和填充。

例如,您有UserComment

// api/models/User.js 
module.exports = { 
    attributes: { 
    name: { 
     type: 'string' 
    }, 
    comments: { 
     collection: 'Comment', 
     via: 'user' 
    } 
    } 
}; 

// api/models/Comment.js 
module.exports = { 
    attributes: { 
    text: { 
     type: 'string' 
    }, 
    user: { 
     model: 'User', 
     via: 'comments' 
    } 
    } 
}; 

那麼你就執行User.find()並獲得已經從數據庫中加入\填充表。

但是,如果要執行手動加入,則可以在Model實例上使用.populate()方法。例如:

// api/controllers/AnyController.js 
module.exports = { 
    action: function(req, res) { 
    User 
     .findOne('ID_HERE') 
     .populate('comments') 
     .then(function(result) {}) 
     .catch(function(error) {}); 
    } 
}; 

你可以閱讀更多關於populate這裏 - http://sailsjs.org/documentation/reference/waterline-orm/queries/populate