簡體   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