返回子相关表的子项(TaskList> TaskListItem> Vehicle)

时间:2017-10-16 12:01:04

标签: knex.js bookshelf.js

考虑下面的模型,我需要返回所有TaskLists TaskListItems(1-N),最后一个Vehicle(N-1,车辆可以)有一个或多个TaskListItems,但没有车辆就不能存在TaskListItem

任务列表

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');
    },
    items: function() {
        return this.hasMany('TaskListItem', 'tasklistId');
    },
    status: function () {
        return this.belongsTo('StatusTaskList', 'statusId');
    }
});

TaskLists = bookshelf.Collection.extend({
    model: TaskList
});

module.exports = {
    TaskList: bookshelf.model('TaskList', TaskList),
    TaskLists: bookshelf.collection('TaskLists', TaskLists)
};

TaskListItem

let bookshelf = require('./base');

var TaskListItem,
    TaskListItems;

TaskListItem = bookshelf.Model.extend({
    tableName: 'tasklist_items',
    createdByUser: function () {
        return this.belongsTo('User', 'createdBy');
    },
    vehicle: function() {
        return this.belongsTo('Vehicle', 'vehicleId').through('VehicleAnomaly', 'vehicleAnomalyId', 'vehicleId', 'id');
    }
});

TaskListItems = bookshelf.Collection.extend({
    model: TaskListItem
});

module.exports = {
    TaskListItem: bookshelf.model('TaskListItem', TaskListItem),
    TaskListItems: bookshelf.collection('TaskListItems', TaskListItems)
};

车辆

let bookshelf = require('./base');

var Vehicle,
    Vehicles;

Vehicle = bookshelf.Model.extend({
    tableName: 'vehicles'
});

Vehicles = bookshelf.Collection.extend({
    model: Vehicle
});

module.exports = {
    Vehicle: bookshelf.model('Vehicle', Vehicle),
    Vehicles: bookshelf.collection('Vehicles', Vehicles)
};

我已经能够使用TaskLists返回所有TaskListItems,但我不知道如何在结果中添加第三级。

new TaskList()
            .query(qb => {
                qb.join('tasklist_items', 'tasklists.id', '=', 'tasklist_items.tasklistId');
                qb.where('tasklist_items.statusId', '=', 1);
            })
            .fetchAll({
                withRelated: [ {
                    'items': function(qb) {
                        qb.where('statusId', '=', 1);
                    }
                }]
            })

如何返回TaskListsTaskListItems以及Vehicle的每个TaskListItem

1 个答案:

答案 0 :(得分:0)

刚刚找到答案。我正在分享以防其他人需要这个:

new TaskList()
            .query(qb => {
                qb.join('tasklist_items', 'tasklists.id', '=', 'tasklist_items.tasklistId');
                qb.where('tasklist_items.statusId', '=', 1);
            })
            .fetchAll({
                withRelated: [ {
                    'items': function(qb) {
                        qb.where('statusId', '=', 1);
                    },
                    'items.vehicle': function(qb) {
                        qb.orderBy('location', 'ASC');
                    }
                }]
            })

解决方案是在withRelated数组中添加一个新项,指向另一个子模型的子模型(vehicle)。