问题描述
我有2个实体:火车
和车站
每列火车都在多个车站停靠,即火车< --->站
。
火车停靠的站点列表与其他火车停靠的站点列表不同。
Each train stops at multiple stations, i.e. Train <--->> Station
.The list of stations a train stops at is different from the list of stations some other train stops at.
如何正确保存并获取列表
How do I correctly save and fetch the list of stations for a particular train?
谢谢
推荐答案
这是吗?
Train <--->> Station
意味着您有一对多的关系?如果是这样,除非每个站只能停一列火车,否则它是行不通的。我要做的是创建第三个实体,称其为停止,具有两个关系,一对多到火车
,多对一个到 Station
。看起来就像
mean you have a one to many relationship? If so, it doesn't work, unless only one train can stop at each station. What I would do is create a third entity, call it say "stop" with two relations, a many to one to Train
and a many to one to Station
. It wil look something like
+-----+ +-------+
|Train| |Stop | +-------+
+-----+ +-------+ |Station|
|stops|<---->>|train | +-------+
+-----+ |station|<<----->|stops |
+-------+ +-------+
要查找特定火车停靠的所有车站,只需查看火车
的车站
属性即可, 停止
对象的 NSSet
个对象,每个对象都有一个属性,该属性是火车停靠的车站。
To find all the stations a particular train stops at, just look at the stops
property of the Train
which will be an NSSet
of Stop
objects each of which has a property which is a station the train stops at.
要添加新的停靠点,只需创建一个新的停止
对象并设置其火车
和站
属性。
To add new stops, just create a new Stop
object and set its train
and station
properties appropriately.
这篇关于核心数据一对多关系:获取特定的相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!