2016-04-21 52 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": {} 
}