2015-08-31 92 views
0

我有複雜的關係,包括2種型號。我想創建解決我的目的的自定義方法。我有MySQL數據庫作爲datasouce。與2種型號的複雜關係

3主要型號。

藝術家,專輯,歌曲。 N的關係通過模式「曲目」:

專輯和歌曲相關2彼此用N。藝術家和曲目通過完整的模型「曲目藝術家」連接。 我希望通過這種關係獲得特定藝術家的專輯和歌曲列表。雖然我用補丁實現,但它返回重複列表,同時我想要唯一的記錄列表。請幫忙。

我只是在這裏提到模型關係對象而不是完整的模型對象。

artist.json

{ 
    "name": "Artist",  
    "relations": { 
     "tracklists": { 
     "type": "hasMany", 
     "model": "Tracklist", 
     "foreignKey": "", 
     "through": "TracklistArtist" 
     } 
    }  
    } 

album.json

{ 
"name" : "Album", 
"relations": { 
    "songs": { 
     "type": "hasMany", 
     "model": "Song", 
     "foreignKey": "", 
     "through": "Tracklist" 
    } 
    } 
} 

song.json

{ 
"name" : "Song", 
    "relations": { 
    "albums": { 
     "type": "hasMany", 
     "model": "Album", 
     "foreignKey": "", 
     "through": "Tracklist" 
    } 
    } 
} 

tracklist.json

{ 
    "name": "Tracklist", 
    "relations": { 
    "album": { 
     "type": "belongsTo", 
     "model": "Album", 
     "foreignKey": "" 
    }, 
    "song": { 
     "type": "belongsTo", 
     "model": "Song", 
     "foreignKey": "" 
    }, 
    "artists": { 
     "type": "hasMany", 
     "model": "Artist", 
     "foreignKey": "", 
     "through": "TracklistArtist" 
    } 
    } 
} 

曲目列表-artist.json

{ 
    "name": "TracklistArtist", 
    "relations": { 
    "artist": { 
     "type": "belongsTo", 
     "model": "Artist", 
     "foreignKey": "" 
    }, 
    "tracklist": { 
     "type": "belongsTo", 
     "model": "Tracklist", 
     "foreignKey": "" 
    } 
    } 
} 

我已經檢查從環回的MySQL的連接器修補。這裏是鏈接。 Check Here

Artist.tracklists({ 
       id: $stateParams.artistId, 
       filter: { 
        groupBY:"albumId", 
        include: {relation: "album", scope: {}}, 
       } 
      }).$promise.then(function (data) { 
      }); 
+0

能告訴你的實際代碼/查詢? http://stackoverflow.com/help/mcve – Cyrbil

+0

你需要關係模型的json嗎? – Sankalp

+0

這可能會有所幫助,加上您用來獲取相關藝術家專輯和歌曲列表的相關部分代碼 – Cyrbil

回答

0

您propably需要您鏈包含獲得的東西,如每一個關係:

Artist.find({ 
    include: { 
    relation: 'Tracklist', 
    scope: { 
     include: { 
     relation: ['Album', 'Song'] 
     } 
    } 
    }, 
    where: {id: 12345} 
}, function() { ... }); 

編輯:

Artist.find({ 
    include: { 
    relation: 'Tracklist', 
    scope: { 
     include: { 
     relation: ['Album'] 
     }, 
     scope: { 
      include: { 
      relation: 'Tracklist', 
      scope: { 
       include: { 
        relation: ['Song'] 
       } 
      } 
     } 
    } 
    }, 
    where: {id: 12345} 
}, function() { ... }); 
+0

我試過了,它給了我「重複」的重複條目。因爲我有很多曲目列表。我需要專輯和歌曲的獨特列表。 – Sankalp

+0

從你的計劃中,一首歌曲可能在許多專輯中,所以如果某首歌曲在許多專輯中被引用,那麼您會得到多張專輯的專輯是正常的,不是嗎? – Cyrbil

+0

您提到的課程關係是正確的。藝人在專輯中演唱多首歌曲。同時,他在不同的專輯中唱過同一首歌。沒關係。 這就是我想要的邏輯。我需要「專輯和歌曲的獨特單獨列表」。我之前寫的補丁與你給出的例子是一樣的。我嘗試過,但我希望創建自定義函數的唯一列表。 – Sankalp