繁体   English   中英

查询多对多关系:按名称获取带有标签的所有产品

[英]Querying many-to-many relationship: get all products with tag by name

这实际上应该很容易做到,但是我还没有做到。

假设我们有一个Model Product 一个Product可以有多个Tag 关系的定义如下:

common/models/product.json

{
  "name": "Product",
  #other stuff
  "relations": {
      "tags": {
      "type": "hasMany",
      "model": "Tag",
      "foreignKey": "productId",
      "through": "ProductTags"
    }
  },
  #more stuff
}

common/models/product-tags.json

{
  "name": "ProductTags",
  #more stuff
  "relations": {
    "product": {
      "type": "belongsTo",
      "model": "Product",
      "foreignKey": "productId"
    },  
    "tag": {
      "type": "belongsTo",
      "model": "Tag",
      "foreignKey": "tagId"
    }   
  },  
  #more stuff
}

common/models/tag.json

  {
      "name": "Tag",
      #more stuff
       "properties": {
         "name": {
         "type": "string",
         "required": true,
         "index": {
         "unique": true
       }
      },
      "relations": {
        "pictures": {
          "type": "hasMany",
          "model": "Pictures",
          "foreignKey": "tagId",
          "through": "PictureTags"
        }
        "products": {
          "type": "hasMany",
          "model": "Product",
          "foreignKey": "tagId",
          "through": "ProductTags"
        }
      },
      #more stuff
    }

如何按标签名称查询所有产品? 例如,获得所有带有标签名称“ XYZ”的产品(如果可以使用“ like”,甚至更好,这样查询就不会完全匹配,而是“ like”)。 首选REST查询格式。

我尝试查看: https : //docs.strongloop.com/display/public/LB/Include+filter但是我的include过滤器会首先返回所有产品,并为其添加标签信息。

编辑:一些尝试:

curl --globoff http://localhost:3000/api/v1/Tags?filter[where][name][inq]=FirstTag&filter[where][name][inq]=ThirdTag&filter[include][products]

这是直接从文档返回错误:

name属性包含无效子句{\\“ inq \\”:\\“ FirstTag \\”} \\

curl --globoff http://localhost:3000/api/v1/Products?filter=[include][tags][name]=NonExistingTag

仍然返回所有产品

curl -X GET "http://localhost:3000/api/v1/Products?filter[where]tags][name]=First" --globoff

返回此错误:{“ error”:{“ name”:“ TypeError”,“ status”:500,“ message”:“无法读取未定义的属性'type'”,“ stack”:“ TypeError:无法读取属性'在PostgreSQL.toColumnValue(/home/fabio/prj/fapl/src/loopback/node_modules/loopback-connector-postgresql/lib/postgresql.js:432:11)中未定义的类型\\ n \\ n

我也看了看: https : //docs.strongloop.com/display/public/LB/Where+filter但找不到任何相关示例... where子句用于产品对象本身,而不用于相关模型。

如果您的模型设置正确,您应该可以执行以下操作

//...snip...
Tag.findById(tagId, function(err, tag){

  // works for multiple relation types
  // hasMany, hasAndBelongsToMany, etc
  tag.products({}, function(err, productsWithTag) {
    if(err) return cb(err);
    // productsWithTag available here
  });

});
//...snip...

哪个应返回该标签的所有产品。 可以用find(filter...)代替findById来通过名称或查询获取标签实例。

请参阅底部的https://docs.strongloop.com/display/public/LB/HasAndBelongsToMany+relations ,以获取“添加到模型的方法”的指导。 还请检查您的模型json是否正确,并确定它是hasAndBelongsToMany还是hasManybelongsTo的组合效果最好。

我也在loopbackjs谷歌组上问了这个问题。

有人(努巴·普林西加利,如果您在这里,则应发布解决方案作为答案,以便我可以接受...)发布此答案:

假设您的代码位于/ api / tags,只需在/ api / tags?filter = {“ where”:{“ id”:“ 1234”},“ include”:“ products”}“发出HTTP GET请求即可where子句可通过like或regex满足您的需求。

如果返回了多个标签,则每个标签都有其自己的产品列表。 如果您只需要一个产品列表,则可以在客户端上将它们合并,或分两个步骤进行查询:找到与where过滤器匹配的所有标签,包括相关的ProductTag,然后使用这些标签来构建您的产品ID列表然后,使用inq运算符查找所有产品。 https://docs.strongloop.com/display/public/LB/Where+filter#Wherefilter-inq 无需自定义端点。

为了获得产品而必须查询标签有点不直观。 另外,如果一个产品分配了多个标签,而我要查询多个标签,则我会多次获得同一产品。

并不理想,但是暂时还可以!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM