在核心数据中跨实体获取和排序
假设我有一个与下面类似的模型,我需要获取按 personRole.roleWeight 排序的特定 Company.companyName 的所有“Person”
这是我目前拥有的模型:
- Entity: Company
- Attributes: companyName
关系:companyRole
实体:角色
- 属性:roleName、roleWeight
关系:rolePerson、RoleCompany
Entity : Person
- 属性:personName
- 关系:person Role
这是一个简单的关系图:
Company --<角色>--<人
有办法做到这一点吗? 如果我需要修改模型,我会很乐意这样做。欢迎所有建议。
谢谢,
Lets say I have a model similar to the one below, and I need to Fetch all 'Person' of a specific Company.companyName sorted by personRole.roleWeight
This is the model I have at the moment:
- Entity: Company
- Attributes: companyName
Relationships: companyRole
Entity: Role
- Attributes: roleName, roleWeight
Relationships: rolePerson, RoleCompany
Entity: Person
- Attributes: personName
- Relationships: person Role
Here is a simple diagram of the relationship:
Company --< Role >--< Person
Is there a way to do this?
If I need to modify the model I would be happy to do so. All suggestions are welcome.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法按角色权重排序,因为可能有多个适合的角色。
您也无法从角色(而不是人员)获得它,因为角色和人员之间存在多对多关系。
您应该重新考虑您的设计,因为那里的多对多没有多大意义。进行一点数据反规范化,将多对多更改为一对多并复制角色名和角色权重值即可解决该问题。
更新
假设您将设计更改为:
那么解决方案变得更加容易:
您基本上获取按权重排序并按公司名称过滤的角色实体。然后,您可以从该角色实体数组中使用 KVC 收集关系另一端的所有人员对象,该关系将按顺序检索它们。
You can't sort by role weight because it is possible to have more than one role that fits.
You also can't come at it from the Role (as opposed to the Person) because you have a many-to-many between role and person.
You should re-think your design because having that many-to-many there does not make much sense. A little bit of data de-normalization, changing that many-to-many to a one-to-many and duplicating the rolename and roleweight values would solve the issue.
Update
Assuming you changed the design to:
Then the solution gets much easier:
You basically fetch the Role entities sorted by weight and filtered by the company name. From that array of Role entities you then use KVC to gather all of the person objects on the other end of the relationship which will retrieve them in order.
角色是中间的猴子而不是人,有什么原因吗?这将使此任务变得更容易,但也许您正在使用排除此选项的数据做其他事情。如果每个人都有一个角色和一个公司,那么您可以为 role.roleWeight 创建一个排序描述符,并在相关公司的员工关系集上使用 NSSet 的
sortedArrayUsingDescriptors:
方法。这将为您提供一个新的排序数组,其中包含按角色权重排序的附加到给定公司的所有人员实体的列表(您可能需要包含辅助排序描述符来处理匹配的角色权重)。Is there a reason that Role is the monkey in the middle and not person? It would make this task easier but maybe you have other things you are doing with the data that precludes this option. If each person had one role and one company you could then create a sort descriptor for role.roleWeight and use NSSet's
sortedArrayUsingDescriptors:
method on the Company in question's employees relationship set. That would give you a new sorted array with a list of all the Person entities attached to a given Company sorted by roleWeight (you might want to include a secondary sort descriptor to take care of matching roleWeights).