1

我如何定义 hasMany Space -> Accounts 关系?

var Space = Bookshelf.Model.extend({
    tableName : 'spaces',
    // Account variable does not exist :/
});

var Account = Bookshelf.Model.extend({
    tableName : 'accounts',
    spaceId   : function() {
        return this.belongsTo(Space);
    },
});

定义这个的正确方法是什么?

PS书架js库没有标签:http: //bookshelfjs.org/

4

1 回答 1

8

根据Docs,这应该有效:

    var Account = Bookshelf.Model.extend({
        tableName : 'accounts'
    });

    var Space = Bookshelf.Model.extend({
        tableName : 'spaces',
        accounts  : function() {
            return this.hasMany(Account, 'spaceId'); // spaceId is foreign key for Account table
        }
    });
于 2013-08-13T11:27:31.603 回答