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.

Data Model Questions /

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

Define a relationship to a many_many relationship


Go to End


522 Views

Avatar
Swift DevLabs

Community Member, 1 Post

30 March 2017 at 9:03pm

In summary, I have 3 models here:

- Level (Primary, Secondary and University etc... A level can have many subjects)
- Subject (Science, English, Mandarin, Chemistry and etc... A Subject belongs to many Level)
- Tutor (The tutor... A Tutor can teach many subject on different Level)

class Level extends DataObject
{
    private static $db = array(
        'Title'     => 'VARCHAR(50)',
    );
    private static $many_many = array(
        'Subjects'  => 'Subject',
    );
}

class Subject extends DataObject
{
    private static $db = array(
        'Title'     => 'VARCHAR(50)',
    );
    
    private static $belongs_many_many = array(
        'Levels'    => 'Level',
    );
}

Above is the 2 DataObjects but I am a little stucked on the Tutor DataObject. I don't think I can do a $has_many to Subject reason being, it needs to directly refer to the relationship because a Tutor might be teaching University-Science and Primary-Science, but not Secondary-Science.

I see my database have a Level_Subject table that acts as the many_many link between the 2 object. But how do I relate Tutor to this?