Return 多对多关联中的模型数据及其相关数据
Return the model data and its related data in a many-to-many association
我有以下 tables:
tasklists: id (PK)
users: id (PK)
tasklist_resources: userId (FK to users.id), tasklistId (FK to tasklists.id)
我在他们的模型中这样定义关系:
let bookshelf = require('./base');
var User,
Users;
User = bookshelf.Model.extend({
tableName: 'users',
taskLists: function() {
return this.belongsToMany('TaskList', 'tasklist_resources', 'userId', 'tasklistId');
}
});
Users = bookshelf.Collection.extend({
model: User
});
module.exports = {
User: bookshelf.model('User', User),
Users: bookshelf.collection('Users', Users)
};
还有这个:
let bookshelf = require('./base');
var TaskListResource,
TaskListResources;
TaskListResource = bookshelf.Model.extend({
tableName: 'tasklist_resources',
users: function() {
return this.belongsToMany('User', 'tasklist_resources', 'tasklistId', 'userId');
}
});
TaskListResources = bookshelf.Collection.extend({
model: TaskListResource
});
module.exports = {
TaskListResource: bookshelf.model('TaskListResource', TaskListResource),
TaskListResources: bookshelf.collection('TaskListResources', TaskListResources)
};
然后,如果在加入 table:
中找到该行,我将尝试 return 完整的用户信息
new TaskListResource({
tasklistId: req.params.tasklistId,
userId: req.params.id
})
.fetch({
withRelated: [ 'users' ]
})
.then(result => {
res.status(200).json(result);
})
但它 return 仅 tasklist_resources table 中的值(例如 [{"tasklistId":1,"userId":1}]
)。我错过了什么?
通过使用 TaskList 模型检索与 taskList 关联的资源(用户),使其正常工作。
let bookshelf = require('./base');
var TaskList,
TaskLists;
TaskList = bookshelf.Model.extend({
tableName: 'tasklists',
createdByUser: function () {
return this.belongsTo('User', 'createdBy');
},
resources: function() {
return this.belongsToMany('User', 'tasklist_resources', 'tasklistId', 'userId', 'id');
}
});
TaskLists = bookshelf.Collection.extend({
model: TaskList
});
module.exports = {
TaskList: bookshelf.model('TaskList', TaskList),
TaskLists: bookshelf.collection('TaskLists', TaskLists)
};
然后 select 数据:
new TaskList({
id: req.params.tasklistId
})
.fetch({ withRelated: ['resources'] })
.then(result => {
res.setHeader('X-InlineCount', result.related('resources').length);
res.setHeader('Access-Control-Expose-Headers', 'X-InlineCount');
res.json(result.related('resources'));
})
我有以下 tables:
tasklists: id (PK)
users: id (PK)
tasklist_resources: userId (FK to users.id), tasklistId (FK to tasklists.id)
我在他们的模型中这样定义关系:
let bookshelf = require('./base');
var User,
Users;
User = bookshelf.Model.extend({
tableName: 'users',
taskLists: function() {
return this.belongsToMany('TaskList', 'tasklist_resources', 'userId', 'tasklistId');
}
});
Users = bookshelf.Collection.extend({
model: User
});
module.exports = {
User: bookshelf.model('User', User),
Users: bookshelf.collection('Users', Users)
};
还有这个:
let bookshelf = require('./base');
var TaskListResource,
TaskListResources;
TaskListResource = bookshelf.Model.extend({
tableName: 'tasklist_resources',
users: function() {
return this.belongsToMany('User', 'tasklist_resources', 'tasklistId', 'userId');
}
});
TaskListResources = bookshelf.Collection.extend({
model: TaskListResource
});
module.exports = {
TaskListResource: bookshelf.model('TaskListResource', TaskListResource),
TaskListResources: bookshelf.collection('TaskListResources', TaskListResources)
};
然后,如果在加入 table:
中找到该行,我将尝试 return 完整的用户信息new TaskListResource({
tasklistId: req.params.tasklistId,
userId: req.params.id
})
.fetch({
withRelated: [ 'users' ]
})
.then(result => {
res.status(200).json(result);
})
但它 return 仅 tasklist_resources table 中的值(例如 [{"tasklistId":1,"userId":1}]
)。我错过了什么?
通过使用 TaskList 模型检索与 taskList 关联的资源(用户),使其正常工作。
let bookshelf = require('./base');
var TaskList,
TaskLists;
TaskList = bookshelf.Model.extend({
tableName: 'tasklists',
createdByUser: function () {
return this.belongsTo('User', 'createdBy');
},
resources: function() {
return this.belongsToMany('User', 'tasklist_resources', 'tasklistId', 'userId', 'id');
}
});
TaskLists = bookshelf.Collection.extend({
model: TaskList
});
module.exports = {
TaskList: bookshelf.model('TaskList', TaskList),
TaskLists: bookshelf.collection('TaskLists', TaskLists)
};
然后 select 数据:
new TaskList({
id: req.params.tasklistId
})
.fetch({ withRelated: ['resources'] })
.then(result => {
res.setHeader('X-InlineCount', result.related('resources').length);
res.setHeader('Access-Control-Expose-Headers', 'X-InlineCount');
res.json(result.related('resources'));
})