Jump to:

23364 Posts in 18125 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » Many Many Relationships

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 934 Views
  • matthewjumps
    Avatar
    Community Member
    21 Posts

    Many Many Relationships Link to this post

    Im having a bit of a hard time working out the best way to structure what I think is a $many_many relationship. I have a Business class and an Event class. Businesses can be associated with multiple Events, and Events can be associated with multiple Businesses.

    What Im not sure of is which is the correct class to put the $many_many, and which is the correct class to put the $belongs_many_many? It almost seems as if BOTH classes need a $many_many AND a $belongs_many_many? Is that the correct way to approach it?

  • matthewjumps
    Avatar
    Community Member
    21 Posts

    Re: Many Many Relationships Link to this post

    Anyone have any suggestions? People must have faced this issue before surely?

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: Many Many Relationships Link to this post

    maybe it is like the documentation says... http://doc.silverstripe.org/recipes:many_many-example

    // Defines the join in the referenced class as $belongs_many_many.
    // a new table, (this-class)_(relationship-name), will be created with a pair of ID fields.
    static $belongs_many_many = array('Articles' => 'Article'

    // an Article can have many Category objects associated with it.
    // calling $this->getManyManyComponents('Categories') retrieves the associated Category objects.
    static $many_many = array('Categories' => 'Category'

    I can't remember whether I've ever had trouble doing ->RelationshipName() on either of the two sides... or if it has affected how is appears when scaffolded with modeladmin... but you can try it out and see... those are the things I'd test to see if there was a difference.

    934 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.