2016-01-13 64 views
1

我正在开发一个包含多个(> 2)表的应用程序,我需要在填充方法 填充帆中的多个表waterline orm

Category.js模式

attributes: { 
    CategoryID:{ 
     type:"integer", 
     required:true, 
     primaryKey:true, 
     autoIncrement:true 
    }, 
    SubCategories:{     //REFERING TO SUB-CATEGORY TABLE 
     collection:'SubCategory', 
     via:'CategoryID' 
    }, 
    CategoryName:{ 
     type:"string", 
     required:true, 
     maxLength:50 
    } 
    } 

这是SubCategory.js模式

attributes: { 
    id:{ 
     type:'integer', 
     required:true, 
     primaryKey:true, 
     autoIncrement:true, 
     maxLength:10, 
     columnName:'SubCategoryID' 
    }, 
    CategoryID:{ 
     model:'Category'     //REFERING TO CATEGORY TABLE 
    }, 
    ProductsOfCategory:{     //REFERING TO PRODUCT TABLE 
     collection:'Product', 
     via:'SubCategoryID' 
    }, 
    SubCategory:{ 
     type:"string", 
     required:true, 
     maxLength:50 
    } 
} 

Product.js模式

attributes: { 
    id: { 
     type: 'integer', 
     primaryKey: true, 
     autoIncrement: true, 
     maxLength: 10, 
     columnName:'ProductID' 
    }, 
    SubCategoryID: { 
     model:'SubCategory' 
    }, 
    ProductDetails:{ 
     collection:'ProductDetails', 
     via:'ProductID' 
    }, 
    ProductName: { 
     type: "string", 
     required: true, 
     maxLength: 50 
    } 
} 

ProductDeatils.js模式

attributes: { 
    id: { 
     type: 'integer', 
     primaryKey: true, 
     autoIncrement: true, 
     maxLength: 10, 
     columnName:'ProductDetailID' 
    }, 
    ProductID:{ 
     model:'Product' 
    }, 
    Size:{ 
     type:"string", 
     required:true, 
     maxLength:10 
    }, 
    Color:{ 
     type:"string", 
     required:true, 
     maxLength:10 
    } 
} 

在填充,我能够填充类别和子类别每个类别。

Category.find() 
     .populate('SubCategories') 
     .exec(function(err, categories){ 
      if (err) { 
       console.log(err); 
       return res.json(err); 
      } 
      console.log(categories); 
      res.json(categories); 
     }) 

如何填充的上述所有表中的一个去,使得最终的查询后,我们得到了所有上述细节在一个JSON。

我们得到参加上述所有表的

是具有所有子类类别,其所有产品和所有产品子类有产品的详细信息在一个JSON

+0

ashishkumar - 我的回答有帮助吗?如果您发现它有帮助,请您标记为正确的答案。还有一个(稍微过时但非常相似的)SOF问题。 – arcseldon

回答

1

你问一个很好的问题。目前已在越来越嵌套填入功能为帆,从字面上几十发出请求的和永久居民等大规模兴趣

看看一些历史在这里:

[FEATURE REQUEST] Recursively populate #308 - 我是迟到了,使得2014年10月29日的请求将在历史记录中看到。

据我所知,大多数的谈话最终会聚在这里(一两年帆用户请求的功能之后):

Deep populate #1052(问题仍然开放作为写2016年1月14日

从这个问题的状态我们现在还不清楚。这两个链接的历史确实提出了其他人使用的解决方法。

我的hunch是递归填充不支持开箱即用。

我在使用水线模型与SailsJS关联时所做的工作是使用像async.js这样的包 - 使用类似瀑布的方法来以编程方式显式地填充子关系。您可以将它与覆盖您调用的模型的默认toJSON()的操作相结合,以将它们之间的关系(以编程方式填充)添加到JSON响应中。您也可以选择使用内置的承诺来实现相同的目标。

发现这个(日期,2014)SOF Question它提供了更多的信息。

有人,如果我在最近的Sails或Waterline版本中错过了此功能,请在这里纠正我的错误 - 在任何项目的发行说明中都找不到任何内容,表示支持。