【问题标题】:gimme example to create model relations on StrongLoop给我一个在 StrongLoop 上创建模型关系的例子
【发布时间】:2014-06-15 11:50:38
【问题描述】:

我想在 StrongLoop 上创建模型关系。 我有这种情况:

  1. 类别:
    • id:1
    • 名称:历史
  2. 预订
    • id:1
    • id_category:1
    • title:美国的喧嚣
    • id_publisher:1
    • --------------
    • id:2
    • id_category:1
    • title:american-hustle2
    • id_publisher:2
  3. 出版商
    • id:1
    • 名称:Publisher-1
    • --------------
    • id:2
    • 名称:Publisher-2

我怎样才能创建这样的模型关系? 谢谢。

【问题讨论】:

    标签: model strongloop


    【解决方案1】:

    您应该能够在以下位置找到您需要的东西:

    http://strongloop.com/strongblog/defining-and-mapping-data-relations-with-loopback-connected-models/

    基本上,

    // 1-many
    Category.hasMany(Book, {as: 'books', foreignKey: 'id_category'});
    Publisher.hasMany(Book, {as: 'books', foreignKey: 'id_publisher'});
    
    // belongsTo
    Book.belongsTo(Category, {as: 'category', foreignKey: 'id_category'});
    Book.belongsTo(Publisher, {as: 'publisher', foreignKey: 'id_publisher'}); 
    

    根据您想要导航模型关系的方式,您可以定义一个方向或两个方向。

    【讨论】:

      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多