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

Managing complex data relation ships with children and grandchildren, what am I doing wrong?


Go to End


975 Views

Avatar
BenA

Community Member, 1 Post

20 December 2015 at 9:21pm

I have a Parent -> Child -> Grandchild relationship that look a bit like this

'Menu' has many 'Courses' and 'Meals'
'Course' has many 'Meals' and belongs to many 'Menus'
'Meal' belongs to many 'Courses' and 'Menus'

class Menu extends Page {
    private static $many_many = array(
        'Courses' => 'Course',
        'Meals' => 'Meal'
    );
    ...
}
 

class Course extends DataObject { 
    private static $belongs_many_many = array(
        'Menus' => 'Menu'
    );
    private static $many_many = array(
    	'Meals' => 'Meal'
    );
    ...
}
 

class Meal extends DataObject {
     private static $belongs_many_many = array(
     	'Menus' => 'Menu',
     	'Courses' => 'Course'
    );
    ...

In the Menu.ss page template I loop through 'Courses' and within that, I loop through Meals.
This gives me the correct courses associated to the menu, the correct meals associated to the course, but ignores the meals relationship to the menu.

How do I get the correct meals associated to the course and menu?