开发者

Adding the same object to multiple to-many relationships

开发者 https://www.devze.com 2023-03-16 00:18 出处:网络
Have a core data question that I am trying to solve for the past couple of hours (5hrs). I have two entities: Student and Class.

Have a core data question that I am trying to solve for the past couple of hours (5hrs).

I have two entities: Student and Class.

Student attributes: name, grade Class attributes: name

Relationships:

  1. Student <<------>>Class, both have to-many relationship.
  2. I create student 1.
  3. Then create Class 1, Class 2.
  4. I then add Student 1 to Class 1
  5. I then add Student 1 to Class 2.

Now when I change the grade of student 1 via the Class 1 its also change the grade for Class 2. So how do I solve this problem. Thanks a bunch.

UPDATE (1): According to answers, I have开发者_StackOverflow modeled the following:

Adding the same object to multiple to-many relationships

I want to add the same student to different classes: So is the relationship between Student and Class right?


Create a new Class to hold the Grade for a Student and Grade. Something like StudentClassGrade or whatever


I would model it as

Grade:

  • attribute: gradeValue (0-100)
  • relationship: student

A Class contains a collection of these

  • relationship: grades

it is highly suggested all relationships in core data have a reverse relationship, thus:

Grade:

  • (reverse) relationship: class

Student:

  • (reverse) relationship: grades

Of course, what's "forward" and what's "reverse" it purely semantics.


The complete answer, thanks to all the comments above and also atomicbird #iphonedev irc channel:

To add the same student to different classes and have or edit the student's grade pertaining to each class: the model is:

Adding the same object to multiple to-many relationships


If I am assuming it right, for each class, student has a grade. If that is the case then you should have student_class and student_class_grade models. student_class ---one to many---student_class_grade.

0

精彩评论

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