Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo

Many-to-Many with Extra Field


Go to End


2 Posts   3142 Views

Avatar
baontq

Community Member, 1 Post

11 December 2008 at 3:50am

Edited: 11/12/2008 3:50am

Hi all,

I am learning silverstripe by implement a simple school management system. I need help on this relationship issue:

A Class has many assigned Teachers, each teacher takes care a subject. So the relationship table should has a classID, teacherID and an extra field is subjectID. How can I implement the many-to-may relationship which has extra field like this one?

Thanks for help!

Avatar
Hamish

Community Member, 712 Posts

11 December 2008 at 7:59am

Really what you have is a three way many-many relationship (*snicker*) between Teachers, Classes and Subjects. The best way to bring them all together is to create a new class that has one of each. Eg:

<?php
class SchoolClassSubject {

  static $has_one {
    'MyClass' => 'SchoolClass',
    'MyTeacher' => 'SchoolTeacher',
    'MySubject' => 'SchoolSubject'
  }
}
?>

And, of course, SchoolClass, SchoolTeacher and SchoolSubject all have a has_many relationship with SchoolClassSubject.