按相关列值订购Bookshelf.js获取 [英] Order Bookshelf.js fetch by related column value

查看:62
本文介绍了按相关列值订购Bookshelf.js获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Bookshelf.js/Knex.js,获取具有相关子模型(称为公司)的模型(称其为用户).
我可以按子项上的字段进行订购吗?型号-company.name?

I'm using Bookshelf.js/Knex.js, fetching a model (call it user) with a related child model (call it company).
Can I order by a field on the child model - company.name?

此外,如果可以的话,我可以进行多种排序,例如company.name降序,然后lastName升序

Also, if that's possible, can I multi sort, say company.name descending then lastName ascending

这是我当前的代码,仅适用于根模型字段. qb.orderBy('company.name', 'desc')不起作用.

Here's my current code, which only works on root model fields. qb.orderBy('company.name', 'desc') doesn't work.

users.query(function(qb) {
  qb.orderBy('lastName', 'asc');
})
.fetch({withRelated: ['company']})
.then(success, error);

推荐答案

尝试以下操作:

users
.fetch({withRelated: [
     {
         'company': function(qb) {
             qb.orderBy("name");
         }
     }
]})
.then(success, error);

我从 https://github.com/tgriesser/bookshelf/issues/361了解了这个想法

这篇关于按相关列值订购Bookshelf.js获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆