开发者

Manually creating intermediate table in a many to many relationship for Core - Data

I'm currenty working with Core-data for an iPhone project.

But I'm a bit confused about one element.

With Core Data currently you do not need to create the intermediate table when creating many to many relationships (its all handled behind the scenes by core data)

But in my case I actually need some attributes on my many to many relationship!

For example

I have a table called Students and another table called Lessons

a Student can be in many lessons and a lesson can have many students

Now a standard many to many relationship will not work for me as I actually need to define more details on the join, i.e. StartDate and LeaveDate.

In a standard sql model for example my join table would be something like

StudentLessons (Studentid, LessonId, StartDate, LeaveDate )

I would need these properties as when i'm querying for information I will need the details from the join to filter my results.

How can I create this in core data and also filter for results?

I've seen folks say that you would actually create the StudentLesson entity开发者_StackOverflow社区 manually in core data.

Now if I did this would I just have the attributes (Startdate, LeaveDate) and then a one to many relationship from the Student and then the Lessons table?

Student - > StudentLessons
Lesson - > StudentLessons

I guess I'm a bit confused on how I would go about making sure that the relationships and the content of the relationships are setup correctly. (i.e If I add an Student object to the StudentLessons - how would I then assign/add the Lesson.)

Sorry this is my first time playing with Core Data.

Takes a bit o getting used to when coming from a full on sql background.


You are absolutely right. The correct way to do this is to create a new entity like StudentLessons. Let's call it Attendance. It should have the startDate and endDate, and two relationships.

The relationship to the student can be many-to-many, unless it is foreseeable that startDate and endDate are always different for each student. One Attendance with its dates can have many students in it. One student can have several Attendance duties.

Student <<---->> Attendance

Clearly, the relationship to Lesson should be one-to-many. One Lesson can have different Attendance configurations, with different dates. But each Attendance belongs only to one Lesson.

Lesson <---->> Attendance

To address your question, you can make the Attendance attribute of Lesson non-optional (and vice versa), this way it will ensure that each Lesson has at least one Attendance with appropriate dates, and each Attendance has exactly one Lesson.

I think your can remove the link between Student and Lesson. Just assign an Attendance rather than a lesson. If you want a Lesson assigned to a Student without dates, just allow Attendance to have NULL as those properties.


TheTiger,

Just because Core Data will create a join table for you, that doesn't mean you have to use it. Maintaining which student succeeds with which lesson is just the same except you will create the intermediate entity and then use the appropriate setters to build the relationships.

You will have to use more key paths and do relationship prefetching but those are straightforward to do.

Andrew

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜