Bookshelf.js的实现,如何从第四张表/模型中获取数据?

时间:2018-09-20 10:30:40

标签: node.js bookshelf.js

这是问题所在:-

表实境     Cusomer -> hasMany Threads -> hasMany messages -> hasOne User (Who sent the message)

我能够通过线程和线程的消息获取客户信息,但是无法在该线程中获取发送该消息的用户信息。 我如何以相同的关系或方式获取此信息?

// Controller
ChatCustomer
.where({
    'anonymous_id': anonymous_id,
    'company_id' : chatApplicationDetails.company_id
})
.fetch({withRelated: ['thread', 'thread.messages']});

获取thread.messages.customer信息需要什么?

// Customer model
var ChatCustomer = db.Model.extend({
    tableName: 'customers',
    hasTimestamps: true,
    hasTimestamps: ['created_at', 'updated_at'],
    company: function() {

        return this.belongsTo('Company');
    },
    thread: function(){

        return this.hasOne('ChatThread', 'customer_id');
    }
});

// Thred model
const ChatThread = db.Model.extend({
    tableName: 'threads',
    hasTimestamps: true,
    hasTimestamps: ['created_at', 'updated_at'],
    company: function () {

        return this.belongsTo('Company');
    },
    customer: function () {

        return this.belongsTo('ChatCustomer');
    },
    messages: function () {

        return this.hasMany('ChatMessage', 'thread_id');
    }
});

// Messages model
const ChatMessage = db.Model.extend({
    tableName: 'messages',
    hasTimestamps: true,
    hasTimestamps: ['created_at', 'updated_at'],
    thread: function () {

        return this.belongsTo('ChatThread');
    },
    user: function () {

        return this.hasOne('User', 'entity_id');
    }
});

0 个答案:

没有答案