2016-03-08 60 views
1

我有兩個表發佈和共享,發佈有很多份額。我想使用userId(由所有者用戶發佈)獲取發佈表中的所有數據,並使用相同的userid來檢查共享表,如果某個人向其他用戶共享帖子,如果任何條件爲真,我需要獲取數據。查詢從一個表中提取信息基於其他在strongloop

我想獲取發佈表中的數據,如果發佈的所有者或共享表中的其他用戶共享。

實施例:

表名:交

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

表名:份額

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

預期輸出:例如通過用戶ID查找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 
     } 
     } 
    } 
    } 

} 

回答

0

約跳過「正式」關係,只是使用的操作鉤(https://docs.strongloop.com/display/public/LB/Operation+hooks)什麼使得當你做一個發現,你可以操縱的結果?

+0

你的意思是單獨調用post和share,最後連接兩個數據? –

+0

我的意思是 - 當調用查找操作時,理論上可以將共享帖子附加到它。 –

相關問題