我有两个表,我使用'withRelated'查询bookshelfjs。如何按相关表中的列对结果进行排序?例如,我想按rankingLinks.id
.fetchAll({withRelated: ['tickerSymbolLinks.tickerSymbol', 'rankingLinks', 'logoLinks.logo']})
看起来很简单但是我的头靠在墙上......
答案 0 :(得分:3)
您实际上可以通过withRelated
订购内容,但是,它只会在此特定属性中对结果进行排序。即:
new Participant().query(function(qb) {
qb.orderBy('participants.id', 'desc'); // You don't have access to any "withRelated" models
}).fetchAll({
withRelated: [{
'courses': function(qb) {
qb.orderBy('courses.id', 'desc');
}
}]
}).then(function(result) {
res.json(result.toJSON());
});
此代码返回所有参与者,按其ID(降序)排序。在这个结果中,我按课程订购课程。结果如下:
[
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"courses": [
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"_pivot_participant_id": 3,
"_pivot_course_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 3,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"courses": [
{
"id": 2,
"course_name": "Android development",
"duration": 60,
"_pivot_participant_id": 2,
"_pivot_course_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 2,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"courses": [
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"_pivot_participant_id": 1,
"_pivot_course_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 1,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
}
]
现在,如果我想通过课程订购结果,我必须稍微更改一下查询。即:
new Course().query(function(qb) {
qb.orderBy('courses.id', 'desc');
}).fetchAll({
withRelated: ['participants']
}).then(function(result) {
res.json(result.toJSON());
});
这会产生略微不同的结果,但需要有所需的顺序:
[
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"participants": [
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"_pivot_course_id": 3,
"_pivot_participant_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 3,
"_pivot_participant_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 2,
"course_name": "Android development",
"duration": 60,
"participants": [
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 2,
"_pivot_participant_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"participants": [
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"_pivot_course_id": 1,
"_pivot_participant_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 1,
"_pivot_participant_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 1,
"_pivot_participant_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
}
]
我希望这有点帮助!