iOS iPhone 获取一对多实体

发布于 2024-11-07 09:29:29 字数 133 浏览 2 评论 0原文

基本上我有一个包含食物的实体和另一个包含酒的实体。每道菜都有推荐的酒。我需要能够选择一道菜并查看该菜品附带的葡萄酒推荐。不同的菜肴可以推荐相同的酒。

通常在 SQL 中我会创建一个链接表来实现这一点,但我在这里有点困惑,有人可以帮忙吗?

Basically I have an entity that contains food dishes and another that contains wine. Each dish has wine recommendations. I need to be able to select a dish and see the wine recommendations that come with that dish. Different dishes can have the same wine recommended for it.

Normally in SQL I would create a link table to achieve this but I am a bit stumped here, can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花海 2024-11-14 09:29:29

核心数据在幕后为您处理“对多”关系的细节,您不需要创建中间表。

打开核心数据 .xcdatamodel 文件,选择 Food 实体,然后单击添加关系。将关系命名为 wines。使其目标实体成为 Wine 实体。打开数据模型检查器 (option-cmd-3) 并选择对多关系

现在选择 Wine 实体。创建一个名为 foods 的关系。在数据模型检查器中将关系的Desination设置为Food实体,它的逆关系是wines并将其设置为也是一个对多关系

现在,Food 的每个实例可以有许多 Wine,而 Wine 的每个实例可以有许多 Food

Core Data handles the details of "to-many" relationships under the hood for you, you don't need to make intermediary tables.

Open your Core Data .xcdatamodel file, select the Food Entity, and click Add Relationship. Name the relationship wines. Make its destination entity the Wine entity. Open the Data Model Inspector (option-cmd-3) and select To-Many Relationship.

Now select the Wine entity. Create a relationship called foods. In the Data Model Inspector make the relationship's Desination the Food entity, it's inverse relationship is wines and set it to be a To-Many Relationship as well.

Now each instance of Food can have many Wines and each instance of Wine can have many Foods.

可遇━不可求 2024-11-14 09:29:29

如果您使用 Core Data,那么您可以简单地在数据库中创建一个 Wine 表,并使用它的主键作为 food-dishs 表中的外键。这个设计中什么不起作用?

If you are using Core Data, then you can simply create a Wine table in the database and use it's primary key as the foreign-key in the food-dishes table. What is not working in this design?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文