2014-09-30 63 views
3

我有一个用户实体和一个Hobbie实体,都在Loopback中定义了他们的模型,我在API浏览器中看到它们。Loopback“hasManyThrough”关系。在哪里添加?

我有一个表UserHobbie链接用户和霍比ManyToMany关系。我tryting声明回环hasManyThrough关系如

User.hasMany(Hobbie, {through: UserHobbie}); 

,但我似乎不能把它做好,为它不会在浏览器露面。我在/server/server.js之后宣布它在bootstrapping部分之后,并且我尝试了在/common/User.js/common/Hobbie.js(但是在其中任何一个中,其他模型都不可见)中执行它。

在User.json或Hobbie.json中是否有适当的语法添加它?这将是我首选的方式,因为我放入json定义中的任何东西都显示在浏览器中。

回答

9

要解决模型JSON中的问题,我将在下面概述解决方案。但是,使用“hasAndBelongsToMany”关系可以更简单地解决您的问题,我也会在下面概述。

在您User.json:

"relations": { 
    "Hobbies": { 
     "type": "hasMany", 
     "model": "Hobbie", 
     "through": "UserHobbie", 
     "foreignKey": "hobbieId" 
    } 
    } 

在您Hobbie.json:

"relations": { 
    "Users": { 
     "type": "hasMany", 
     "model": "User", 
     "through": "UserHobbie", 
     "foreignKey": "userId" 
    } 
    } 

你UserHobbie.json是这样的(请注意,你不中定义用户名字和hobbieId“属性“:

{ 
    "name": "UserHobbie", 
    "plural": "UserHobbies", 
    "base": "PersistedModel", 
    "properties": { 
    "id": { 
     "type": "String", 
     "id": true 
    } 
    }, 
    "validations": [], 
    "relations": { 
    "Users": { 
     "type": "belongsTo", 
     "model": "User", 
     "foreignKey": "userId" 
    }, 
    "Hobbies": { 
     "type": "belongsTo", 
     "model": "Hobbie", 
     "foreignKey": "hobbieId" 
    } 
    }, 
    "acls": [], 
    "methods": [] 
} 

这更简单的方式做到这一点如下:

不要明确创建UserHobbies模型。 Loopback会自动为你创建一个Join模型。

在你的用户模型:

"relations": { 
    "Hobbies": { 
     "type": "hasAndBelongsToMany", 
     "model": "Hobbie" 
    } 
    } 

在你霍型号:

"relations": { 
    "Users": { 
     "type": "hasAndBelongsToMany", 
     "model": "User" 
    } 
    } 

如果你想这样做的代码,你是正确的,也有认为保持这种关系引导时间问题从资源管理器中出现。我将尽快添加另一个答案,向您展示如何完成这项工作。

+2

谢谢!你是否以某种方式与StrongLoop相关?因为,在这种情况下,我希望你添加回答[hasManyThrough]中的文档(http://docs.strongloop.com/display/LB/HasManyThrough+relations)和[hasAndBelongToMany](http://docs.strongloop.com/display/LB/HasAndBelongsToMany+关系)部门离子,它不具有与这些关系的代码定义类似的json。 – amenadiel 2014-09-30 18:30:24

+1

我们将尽快做到这一点...这是代码/ JSON示例问题很常见,我们将继续努力...感谢您让我过去... – tonyStrong 2014-09-30 18:40:14

+0

尊敬的@ tonyStrong,如果我经历第二个解决方案...我没有用户中的hobbieId,也没有Hobbies中的userId ...怎么可能自动创建一个联接模型? – amenadiel 2014-10-08 15:52:21

3

现在,为了让您的原始基于代码的实现创建关系显示在资源管理器中,这里是你应该做的。

首先,从环回项目的./server/boot目录,将“explorer.js”到./server(其中server.js是在项目中。

./server/server的最后一部分.js文件应该是这个样子(我已经消除了简洁一些意见

boot(app, __dirname); 

... // Removed for brevity 

app.use(loopback.urlNotFound()); 

// The ultimate error handler. 
app.use(loopback.errorHandler()); 

app.start = function() { 
    // start the web server 
    return app.listen(function() { 
    app.emit('started'); 
    console.log('Web server listening at: %s', app.get('url')); 
    }); 
}; 

// start the server if `$ node server.js` 
if (require.main === module) { 
    app.start(); 
} 

现在编辑.server/server.js的最后一部分,看起来像这样:

boot(app, __dirname); 
    // We took explorer.js out of /boot and put it in /server root next to server.js 


var programmaticLoopbackSetup = require('./programmaticLoopbackSetup'); 
    // If the User has any special Programmatic Loopback Setup (create Model Relationships, etc.) do it first 

if (programmaticLoopbackSetup !== undefined) { 
    programmaticLoopbackSetup(app, finishUp); 
} 
else { 
    finishUp(); // If you didn't want any Code based Setup 
} 

// Defer all the rest of the Startup Work until Explorer 
// has all the Model Info it needs from any Async or Programmatic Setup. 
function finishUp() { 

    require('./explorer')(app); 
     // This was formerly done within "boot" above... 

     ... // removed for brevity... 

    // Requests that get this far won't be handled 
    // by any middleware. Convert them into a 404 error 
    // that will be handled later down the chain. 
    app.use(loopback.urlNotFound()); 

    // The ultimate error handler. 
    app.use(loopback.errorHandler()); 

    app.start = function() { 
     // start the web server 
     return app.listen(function() { 
     app.emit('started'); 
     console.log('Web server listening at: %s', app.get('url')); 
     }); 
    }; 

    // start the server if `$ node server.js` 
    if (require.main === module) { 
     app.start(); 
    } 
} 

添加新文件a t。/server/programmaticLoopbackSetup.js,使它看起来像下面(注意它是如何敲定的应用程序初始化

module.exports = function programmaticLoopbackSetup(app, next) { 
    var User = app.models.User; 
    var Hobbie = app.models.Hobbie; 


    // This Block Below Creates a Many to Many User/Hobbie Relationship directly 

     User.hasAndBelongsToMany(Hobbie); 
     Hobbie.hasAndBelongsToMany(User); 

    next(); // Callback to finish App Init... 
}; 

您将看到用户/霍关系之前之后“开机”之称的(使用“hasAndBelongsToMany”这种情况下)在资源管理器中执行WHEREVER基于代码的LoopBack模型,数据源或其他改进programmaticLoopbackSetup.js

+0

谢谢。我相信我可以应付在Explorer中没有显示的方法,只要它在命令行中工作。 – amenadiel 2014-10-02 12:18:38