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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

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

many_many and belongs_many_many to create infinitely nested hierarchy

Go to End

2 Posts   908 Views


Community Member, 2 Posts

6 December 2011 at 2:48pm

Hi there

I am trying to allow admins to create nested criteria to as many levels deep as they like.

Everything extends "Criterion", including "Activity" (things that users have to complete) and "Achievement" (which are rewarded based on the criterion).

"Achievement", since it extends "Criterion", can be nested. This way, admins can create complex structures.

It is all working with has_many just fine. But this means that as soon as I add an "Activity" or "Achievement" to more than one "Criterion", it loses its relationship with the one it was added to first.

So I now have many_many => "Criterion" and belongs_many_many "Criteria" in "Criterion". But it isn't allowing me to add any children, nothing ever makes it into the Criterion_Criteria table...

Anything I should be looking out for?


Community Member, 2 Posts

8 December 2011 at 3:40pm


Was really not a problem with the types of relationships so much as the interaction in forms and one key issue:

If anyone is ever trying to do something similar, just make sure that you write() your child record before you try to add it as a component to it's new parent, because it obviously (in hindsight) doesn't have an ID until it is written.

Also, when you get one level deep into the nesting, you have to make sure that if you are creating one overall form, your form actions have the id of the child you are trying to add another child to, otherwise, you won't have a reference to the object you want to add to.