Parse 以多种方式支持关系:
- 指针(想想外键 ID)
- 指针数组(想想连接表,但只有当你总是想得到所有指针时)
- 关系(想想连接表,您要在其中查询子集)
您使用什么很大程度上取决于您的查询。在您的情况下,我会建议以下内容:
视频:
- 名称(字符串)
- 网址(字符串)
- 季节(指针)
- 游戏(数组,假设这是一个简短的列表)
- 表演者(阵列)
表演者:
- 名称(字符串)
- Bio(字符串,可能带有 HTML 或其他标记)
游戏:
- 名称(字符串)
- 详细信息(字符串)
- 表演者(阵列)
季节:
对于您的查询(在 JavaScript 中,因为您没有指定语言):
// setup bits
var Video = Parse.Object.extend("Video");
var Performer = Parse.Object.extend("Performer");
var Game = Parse.Object.extend("Game");
var Season = Parse.Object.extend("Season");
// All videos where performer = x
var videosForPerformerQuery = new Parse.Query(Video);
// assumes performerX has been retrieved or initialised with an ID
// equalTo in this case will find records where performerX is in the array of performers
videosForPerformerQuery.equalTo('performers', performerX);
// if you need to output the full list of performers:
videosForPerformerQuery.include('performers');
// if you need the full season object:
videosForPerformerQuery.include('season');
videosForPerformerQuery.find({
success: function(results) {
// TODO: handle array of video objects in results
},
error: function(error) {
// TODO: handle error
}
});
// All games where performer = x
var gamesForPerformerQuery = new Parse.Query(Game);
// .. identical to above ..
// All performers where game = x
var gameQuery = new Parse.Query(Game);
gameQuery.include('performers');
// gameObjectID is a string key, e.g. "xWMyZ4YEGZ"
gameQuery.get(gameObjectId, {
success: function(game) {
// TODO: game.performers array should be populated, use it
},
// .. error handler too ..
});
// All videos where season = x
var videosForSeason = new Parse.Query(Video);
videosForSeason.equalTo('season', seasonX);
// .. find same as above examples ..
有时您会需要双向连接。您可以在每一端手动填充一个数组/关系,或者使用一个新对象来保存关系(特别是如果关系包含额外的信息)。例子:
游戏表演者:
根据需要查询它和include
游戏/表演者:
var GamePerformers = Parse.Object.extend("GamePerformers");
var performersForGameQuery = new Parse.Query(GamePerformers);
performersForGameQuery.equalTo('game', gameX);
performersForGameQuery.include('performer');
performersForGameQuery.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
console.log('Performer: '
+ object.get('performer').get('name')
+ ' played role: '
+ object.get('role'));
}
}
});