多对多关系 [英] Many-to-Many relationships

查看:196
本文介绍了多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下具有多对多关系的实体。

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

我制作了一个小型播放列表应用程序,其中名为Entity的实体用于播放列表,歌曲实体用于歌曲。目前我有一个viewcontroller,你可以添加播放列表到实体和viewcontroller,你可以添加歌曲到歌曲实体。问题是所有的视频都显示在每个播放列表中。如何在这两个实体之间挂钩这种关系?

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?

我需要在包含播放列表ID的歌曲中创建属性吗?或者Entity如何知道哪个歌曲属于哪个Entity对象?

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?

推荐答案

首先,我建议选择稍微不同实体
的名称和关系,特别是实体
名称的单数形式(歌曲,播放列表)和目标的复数形式>

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:

现在对象通过设置它们之间的关系来挂钩。
假设您有一首歌曲和一个播放列表:

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;

现在您可以致电

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

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

建立这两个对象之间的关系。
无论你调用哪个,因为歌曲和播放列表
反向关系 songA 添加到 playlistB.songs
playlistB 已添加到 songA.playlists

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.

这篇关于多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆