CoreData:如何建模循环多对多关系
一直试图了解如何在 Xcode 4.2 中对循环核心数据多对多关系进行建模。在 SQL 中,我将使用映射表并自定义 SQL 表达式来为我提供正确的数据,但在核心数据中,我似乎无法理解如何让他正常工作。 我有 3 个相互关联的实体:
- 部门
- 角色
- 人员
我的链接是:
- 部门可以有多个角色 部门
- 可以有多个人员
- 一个角色可以出现在多个部门
- 一个角色可以分配给多个人员
- 一个人可以属于 使用
- 一个人可以担任多个角色
核心数据对此进行建模的最佳方法是什么?哪些实体以及它们之间的关系最合适?您会使用这 3 者之间的映射表,还是仅分配正确的关系?
任何帮助将不胜感激,因为我似乎无法弄清楚这一点......
编辑: 非常感谢您的快速反馈。 不幸的是,我无法上传我的数据模型,因为我是这个网站的新手,不允许发布任何图像。
要读取数据,我目前仅使用这样的谓词:
predicate = [NSPredicate predicateWithFormat:@"personPartOfRoles == %@", myRole];
我似乎无法弄清楚如何维护此对象图以从角色中添加或删除人员。我假设我需要将整个图表加载到内存中,并以某种方式只找到要映射的正确实体。
Been trying to understand how to model a circular core-data many-to-many relationship in Xcode 4.2. In SQL I would be using mapping tables and customize my SQL expressions to give me the right data, but in Core data I can’t seem to understand how to get his to work properly.
I have 3 entities which are all related to each other:
- Department
- Role
- Person
My links are:
- Department can have multiple Roles
- Department can have multiple Persons
- A role can be present at many departments
- A role can have many persons assigned to it
- A person can belong to multiple departments
- A person can be part of many roles
What is the best way to model this using Core data and which entites, with which relationships between them would be most adequate? Would you use mapping tables between these 3, or just assign the correct relationships?
Any help would be highly appreciated as I can't seem to figure this out....
EDIT:
Thank you very much for the fast feedback.
Unfortunately I can't upload my data model as I am new to this site and not allowed to post any images.
To read the data I am currently only using a Predicate like this:
predicate = [NSPredicate predicateWithFormat:@"personPartOfRoles == %@", myRole];
What I can't seem to figure out is how to maintain this object graph to add or remove persons from roles. I have assumed I need to load up the entire graph in memory and somehow find only the correct entities to map in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需在您的实体中分配正确的关系即可。
如果您要使用 Core Data,请务必忽略您存储在 SQLite 中的事实。存储只是您可以忘记的幕后实现细节(因为 coredata 能够存储在 XML 或内存中)。
Just assign the correct relationships in your entities.
If you are going to use Core Data it is important to ignore the fact that you are storing in SQLite. The storage is simply a behind the scenes implementation detail (as coredata is capable of storing in XML or in memory) that you can forget about.
部门<<--->>角色(部门角色)
部门<<--->>人(部门对人)
角色<<-->>人(对人的角色)
使每种关系都有逆向关系。
请记住,核心数据是一个可以持久保存到数据库的对象图。将其视为对象图。
Department <<--->> Role (roles to departments)
Department <<--->> Person (departments to people)
Role <<-->> Person (roles to people)
Make each relationship have an inverse.
Remember that Core Data is an object graph that can persist to a database. Treat it like an object graph.