我有两个表发布和共享,发布有很多共享。我想使用userId(由所有者用户发布)获取发布表中的所有数据,并且还使用相同的userid检查共享表,即是否有一个共享发布给其他用户,如果满足任何条件,则我需要获取数据。

我想在所有者表中发布数据,或者由共享表中的其他用户共享,以便在发布表中获取数据。

范例:

表格名称:发布

id(pk) postname userid
    1  abc       10
    2  xxx       10
    3  yyy       11
    4  zzz       12
    5  bbb       13


表名:share

id postid(fk) userid
1   3           10
2   4           10
3   3           11
4   1           12


预期的输出:示例按用户标识10查找

 id postname userid
  1  abc       10  // this record created by user 10 (owner)
  2  xxx       10  // this record created by user 10 (owner)
  3  yyy       11  // this record shared by other user to user 10.
  4  zzz       12  // this record shared by other user to user 10.


模型关系:post.json

 "relations": {
    "shares": {
      "type": "hasMany",
      "model": "share",
      "foreignKey": "postid"
    }
  }


我尝试了以下查询,但出现错误

{
  "where": {
    "or": [
      {
        "userid": 10
      },
      {
        "include": {
          "relation": "shares",
          "scope": {
            "where": {
              "userid": 10
            }
          }
        }
      }
    ]
  }
}


和//在这里,我也只检查了postname,但是不需要这个问题。

{
  "where": {
    "or": [
      {
        "userid": 10
      },
      {
        "postname": "xxx"
      }
    ],
    "include": {
      "relation": "shares",
      "scope": {
        "where": {
          "userid": 10
        }
      }
    }
  }

}

最佳答案

跳过“正式”关系并仅使用操作钩子(https://docs.strongloop.com/display/public/LB/Operation+hooks),以便进行查找时可以操纵结果呢?

10-06 12:11