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.

General Questions /

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

Many Many Relationships


Reply


3 Posts   991 Views

Avatar
matthewjumps

Community Member, 21 Posts

23 November 2010 at 4:46pm

Edited: 23/11/2010 4:47pm

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?

Avatar
matthewjumps

Community Member, 21 Posts

24 November 2010 at 5:28pm

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

Avatar
swaiba

Forum Moderator, 1799 Posts

25 November 2010 at 9:07am

Edited: 25/11/2010 9:07am

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.