Jump to:

3428 Posts in 1056 Topics by 733 members

Data Model Questions

SilverStripe Forums » Data Model Questions » many_many and belongs_many_many to create infinitely nested hierarchy

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

Page: 1
Go to End
Author Topic: 470 Views
  • Matt_LimRes
    Avatar
    Community Member
    2 Posts

    many_many and belongs_many_many to create infinitely nested hierarchy Link to this post

    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?

  • Matt_LimRes
    Avatar
    Community Member
    2 Posts

    Re: many_many and belongs_many_many to create infinitely nested hierarchy Link to this post

    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.

    470 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.