2016-04-21 48 views
0

我有一個FollowMap如下迴環關係到同型號

{ 
     "name": "FollowMap", 
     . 
     . 
     . 
     "relations": { 
     "follower_acc": { 
      "type": "belongsTo", 
      "model": "Account", 
      "foreignKey": "follower_id" 
     }, 
     "following_acc": { 
      "type": "belongsTo", 
      "model": "Account", 
      "foreignKey": "following_id" 
     } 
     } 
    } 

我想建立一個關係到我的賬戶的模式,這樣我就可以得到一個用戶在隨&下面的列表中,賬戶模型如下;

{ 
    "name": "Account", 
    . 
    . 
    . 
    "followers": { 
     "type": "hasMany", 
     "model": "FollowMap", 
     "foreignKey": "following_id" 
    }, 
    "following": { 
     "type": "hasMany", 
     "model": "FollowMap", 
     "foreignKey": "follower_id" 
    } 
    } 
} 

從關係,我可以取然而一個帳戶的追隨者,以下算,我無法獲取這些關係的追蹤者和追蹤帳戶列表。我希望這很清楚。

回答

1

我知道這個問題可能已經過時,但對於那些仍在尋找解決方案的人,我會給我的。我想要在我的數據庫中實現profils實體之間的發佈者/訂閱者關係。

最明顯的方法來做到這一點是與hasAndBelongsToMany關係,但奇怪的是,你不能改變模型中使用的外鍵:這意味着你不能聲明「publishedId」「subscriberId」用作外鍵引用一個Profil實體。

您必須手動聲明用於引用這兩個外鍵的第三個表。

這第三個表我的工作示例:

//common/models/subscribing.json 
{ 
    "name": "Subscribing", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": {}, 
    "validations": [], 
    "relations": { 
    "subscriber": { 
     "type": "belongsTo", 
     "model": "Profil", 
     "as": "subscriber" 
    }, 
    "publisher": { 
     "type": "belongsTo", 
     "model": "Profil", 
     "as": "publisher" 
    } 
    }, 
    "acls": [], 
    "methods": {} 
} 

我的PROFIL實體通過Subscribings表相關本身:

//common/models/profil.json 
{ 
    "name": "Profil", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": { 
    "name": { 
     "type": "string", 
     "required": true 
    } 
    }, 
    "validations": [], 
    "relations": { 
    "followers": { 
     "type": "hasMany", 
     "model": "Profil", 
     "foreignKey": "publisherId", 
     "keyThrough": "subscriberId", 
     "through": "Subscribing" 
    }, 
    "subscribings": { 
     "type": "hasMany", 
     "model": "Profil", 
     "foreignKey": "subscriberId", 
     "keyThrough": "publisherId", 
     "through": "Subscribing" 
    } 
    }, 
    "acls": [], 
    "methods": {} 
}