Bookshelf.js - 如何从 join table 中获取列?
Bookshelf.js - How to get a column from join table?
我有一个User模型和一个Course模型,它们是多对多的关系,所以基本上用户可以加入多个课程,课程有很多用户(学生)。我有一个连接 table,我正在尝试向连接 table 添加其他信息。基本上,用户在每门课程中都有每月可以提出的问题配额。所以我在 User_Course 连接 table 中添加了一个配额列。我在访问配额列时遇到问题。下面给出相关代码,给个思路。
var User = DB.Model.extend({
Courses: function() {
return this.belongsToMany('Course', 'User_Course', 'userId', 'courseId');
},
});
var Course = DB.Model.extend({
Users: function() {
return this.belongsToMany('User', 'User_Course', 'courseId', 'userId');
}
})
并且 User_Course 是我的连接 table 具有额外的配额列:
**User_Course:**
userId: int
courseId: int
quota: int
我希望能够减少我的配额值。我可以使用 updatePivot 更新它,但我无法简单地获取配额中的值。这是我用来更新值的代码:
var userPromise = new User.User({userId: userId}).fetch({withRelated: ['Courses']});
userPromise.then(function(_user) {
_user.related('enrolledCourses').updatePivot({courseId:courseId, quota:1}).then(function() {
return;
})
})
如您所见,我在这里每次都将配额值更新为 1。我想以编程方式确定当前配额值是多少,然后将其递减。
在我学会更合适的方法之前,我一直在使用 knex.raw 和 mysql 语句来减少配额列。在我的控制器中,我调用 decrementQuota 并传入 userId 和 courseId:
function decrementQuota(userId, courseId) {
new User.User({userId: userId}).decrementQuota(userId, courseId);
}
在我的用户模型中,knex.raw 调用通过减少配额的值来更新 User_Course。
var User = DB.Model.extend({
decrementQuota: function(uid, cid) {
DB.knex.raw('update User_Course set quota = quota -1 where userId=' + uid + ' and courseId=' + cid).then(function(quota) {
});
},
})
尝试withPivot
。例如,return this.belongsToMany(Comment).withPivot(['created_at', 'order']);
你可以把你的配额放在这里,你会在加载关系时得到它。
更新:
好的,这是一个例子:
new Town().where({
town_number: 2301
}).fetchAll({
columns: ['town_title', 'id'],
withRelated: [{
'address': function(qb) {
qb.select('addresses.town_id', 'addresses.participant_id');
qb.columns('addresses.street_name');
// here you could simply do qb.where({something_id: 1}); as well
}
}]
}).then(function(result) {
res.json(result);
});
所以基本上不只是说 withRelated: ['table_name']
,而是将 table 名称定义为对象,将 属性 table_name
和 function
定义为对象值,它有一个查询生成器 (qb
),您可以单独查询 table。
希望对您有所帮助!
我有一个User模型和一个Course模型,它们是多对多的关系,所以基本上用户可以加入多个课程,课程有很多用户(学生)。我有一个连接 table,我正在尝试向连接 table 添加其他信息。基本上,用户在每门课程中都有每月可以提出的问题配额。所以我在 User_Course 连接 table 中添加了一个配额列。我在访问配额列时遇到问题。下面给出相关代码,给个思路。
var User = DB.Model.extend({
Courses: function() {
return this.belongsToMany('Course', 'User_Course', 'userId', 'courseId');
},
});
var Course = DB.Model.extend({
Users: function() {
return this.belongsToMany('User', 'User_Course', 'courseId', 'userId');
}
})
并且 User_Course 是我的连接 table 具有额外的配额列:
**User_Course:**
userId: int
courseId: int
quota: int
我希望能够减少我的配额值。我可以使用 updatePivot 更新它,但我无法简单地获取配额中的值。这是我用来更新值的代码:
var userPromise = new User.User({userId: userId}).fetch({withRelated: ['Courses']});
userPromise.then(function(_user) {
_user.related('enrolledCourses').updatePivot({courseId:courseId, quota:1}).then(function() {
return;
})
})
如您所见,我在这里每次都将配额值更新为 1。我想以编程方式确定当前配额值是多少,然后将其递减。
在我学会更合适的方法之前,我一直在使用 knex.raw 和 mysql 语句来减少配额列。在我的控制器中,我调用 decrementQuota 并传入 userId 和 courseId:
function decrementQuota(userId, courseId) {
new User.User({userId: userId}).decrementQuota(userId, courseId);
}
在我的用户模型中,knex.raw 调用通过减少配额的值来更新 User_Course。
var User = DB.Model.extend({
decrementQuota: function(uid, cid) {
DB.knex.raw('update User_Course set quota = quota -1 where userId=' + uid + ' and courseId=' + cid).then(function(quota) {
});
},
})
尝试withPivot
。例如,return this.belongsToMany(Comment).withPivot(['created_at', 'order']);
你可以把你的配额放在这里,你会在加载关系时得到它。
更新:
好的,这是一个例子:
new Town().where({
town_number: 2301
}).fetchAll({
columns: ['town_title', 'id'],
withRelated: [{
'address': function(qb) {
qb.select('addresses.town_id', 'addresses.participant_id');
qb.columns('addresses.street_name');
// here you could simply do qb.where({something_id: 1}); as well
}
}]
}).then(function(result) {
res.json(result);
});
所以基本上不只是说 withRelated: ['table_name']
,而是将 table 名称定义为对象,将 属性 table_name
和 function
定义为对象值,它有一个查询生成器 (qb
),您可以单独查询 table。
希望对您有所帮助!