user3195388
user3195388

Reputation: 129

Many-to-Many relationships

I have following entities with a many-to-many relationships.

enter image description here

i'm making a small playlist app where the entity named Entity is for the playlists and the songs entity is for the songs. at the moment i have a viewcontroller where you can add playlists to the Entity and a viewcontroller where you can add songs to the song entity. The problem is that all the videos are shown in every playlists. How can i hook this relationship up between these 2 entities?

Do i need to make a attribute in Songs which hold a playlist id? or how will the Entity know which song belong in which Entity object?

Upvotes: 0

Views: 815

Answers (2)

Martin R
Martin R

Reputation: 539685

First of all, I would suggest to choose slightly different names for the entities and relationships, in particular the singular form (Song, Playlist) for the entity name, and the plural form of the destination (songs, playlists) for to-many relationships:

enter image description here

Now the objects are "hooked up" by setting the relationship between them. Assume that you have a song and a playlist:

Song *songA;
Playlist *playlistB;

Now you can either call

[songA addPlaylistsObject:playlistB]; // (1)

or

[playlistB addSongsObject:songA]; // (2)

to establish a relationship between these two objects. It does not matter whether which one you call because "songs" and "playlists" are inverse relationships. songA is added to playlistB.songs, and playlistB is added to songA.playlists.

Upvotes: 2

8bitcat
8bitcat

Reputation: 2224

enter image description hereCheck this This is picture of the answer! You will need to use a table to couple the to tables together.

Upvotes: 0

Related Questions