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

many_many and belongs_many_many to create infinitely nested hierarchy


Go to End


2 Posts   1095 Views

Avatar
Matt_LimRes

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?

Avatar
Matt_LimRes

Community Member, 2 Posts

8 December 2011 at 3:40pm

Solved:

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.