tags:

views:

80

answers:

3

It's very confusing; it seems like I'll need to have at least one many-to-many relationship.

  1. A track might be sung by 2+ artist - how can I store that in the database?
  2. While showing that track, I want link to each artist so that when users click the track it opens that artist's profile page.

Can anyone provide me with some assistance in designing a class or classes to accomplish this?

+1  A: 

Any many-to-many relationship requires three tables. For your example:

Song<br/> 
Name  SongID

Artist <br/>
Name  ArtistID

ArtistsInSongs <br/>
ArtistID SongID

I'll let you figure out the rest.

Matthew Jones
+2  A: 

Try something like that

tblSongs
   SongId
   Title
   YearProduced
   etc.

tblArtist
   ArtistId
   Name
   ProfilePage
   etc..

tblSongArtists
    SongId
    ArtistId

Then your queries could look something like

SELECT Title, YearProduced, Name, ProfilePage
FROM tblSongs  S
LEFT OUTER JOIN tblSongArtists SA ON SA.SongId = S.SongId
LEFT OUTER JOIN tblArtists A ON A.ArtistId = SA.ArtistId
WHERE Title = 'I got the blues'  -- ....
ORDER BY SongId  -- or Song Title  (*)

(*) order by clause is in case the search criteria produces more than one song, and if you wish to keep the artists for a given song in sequence.
Such a query produces multiple rows for a song with several artists, one row per artists, the values of the columns from tblSongs being repeated.

mjv
i m also adding artist_id in my album table... n also in track table ..m i wrong ?
sunidhi
i think there is no use of artist_id in album table...its getting confusing.
sunidhi
@sunidhi On adding (or not adding) artist_id to album table... That is to be avoided, becasuse I'm assuming it is possible to have _serveral_ artists associated with a given album. You will then either need a tblAlbumArtists table (similar to tblSongArtists), or to select the list of artists [for a given album] indirectly with some' like that: tblAlbums -> tblbSongs -> thlSongArtists -> tblArtists In such a query you would need to use DISTINCT so you only get each artist listed once, even though he/she may be related to several songs of the album.
mjv
i m not able to get it .. :(
sunidhi
plz check my funtion i added in this as answer below
sunidhi
A: 
public static function find_artist_on($track_id=0) {
    global $database;
    $sql = "SELECT * FROM " . self::$table_name ." s " ;
    $sql .= "LEFT OUTER JOIN trackartist TA ON TA.track_id =s.track_id";
    $sql .= "LEFT OUTER JOIN artist A ON A.artist_id =TA.artist_id";
    $sql .= " WHERE s.artist_id=" .$database->escape_value($track_id);
    $sql .= " ORDER BY artist_id ASC";
    return self::find_by_sql($sql);
    }
sunidhi