2016-02-19 117 views
0

我有一些視頻可以被用戶喜歡和用戶評論,所以我需要2個多對多的關聯。續集2多對多的關係

用戶模型

User.belongsToMany(models.Video,{ through: 'user_like_video' }); 
User.belongsToMany(models.Video, { through: 'user_comment_video' }); 

視頻模式

Video.belongsToMany(models.User, {through: 'user_like_video'}); 
Video.belongsToMany(models.User, {through: 'user_comment_video'}); 

我在DB得到的模式是:

enter image description here

的許多視頻之間的許多關係的標籤做工精良我可以使用方法getTags()來檢索所有的t從一個視頻ags,但如果你有2(多對多關係)創建方法?

當我使用user.getVideos()時,我只獲得了最後一個關係,它在這裏是user_comment_video。

回答

0

我終於找到一個辦法:

Video.belongsToMany(models.User, {through: 'user_like_video', as: 'Like'}); 
Video.belongsToMany(models.User, {through: 'user_comment_video',as: 'Comment'}); 

這暴露方法getLike()和getComment()