【问题标题】:SailsJS manually populate records using Model.querySailsJS 使用 Model.query 手动填充记录
【发布时间】:2015-04-29 04:12:31
【问题描述】:

假设我有这个例子作为我的应用程序 http://sailsjs.org/#!/documentation/concepts/ORM/Associations/OnetoMany.html

由于某些重要原因(复杂)我无法使用 Model.populate() 并且我一直在使用 Model.query()

有谁知道如何得到User.find().populate('pets')的结果@请使用Model.query()

谢谢

【问题讨论】:

    标签: sql node.js sails.js waterline


    【解决方案1】:

    您可以像水线适配器一样填充 OneToMany:

    1. 找回父母 :
      select * from user ...
    2. 仅在一个查询中检索每个父级的子级,以免 DBMS 过载:
      select * from pet where user = user1.id union select * from pet where user = user2.id union ... union select * from pet where user = userN.id。
    3. 通过 parentPk 重新组合孩子(你可以使用 lodash 或 underscore.js 函数来做到这一点)例如:

      users.forEach(function(user){ user.pets = _.filter(pets,function(pet){ return pet.user === user.id; }); });

    【讨论】:

    • 嘿,您能否将此代码引用到示例sailsjs.org/#!/documentation/concepts/ORM/Associations/… .. 我不知道以上所有代码的含义。对不起
    • 1.你应该执行.query来检索你想要的用户,2。那么你应该像上面的例子一样构建一个查询来检索一个唯一查询中的所有宠物,最后,使用 3.,你可以将孩子分配给他们正确的父母。
    猜你喜欢
    • 1970-01-01
    • 2019-09-01
    • 2014-03-20
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2014-11-26
    相关资源
    最近更新 更多