Hello SS World :)
I was stumped on this for a while
but I figured it out so I figured I'd share my results.
Update: Nevermind, it doesn't seem to be working. After reading this message, continue to the responses!
Update #2: I found a workaround to get this to work. Make sure you read the whole thread. The workaround is the 7th post.
I was trying to build two classes: Person, Company
A Person can be an Employee of a Company.
A Person can Represent a Company, but not necessarily be an employee of that company.
Both of these relationships would be represented by this table structure (one for each) if it was outside of Silverstripe:
| ID | PersonID | CompanyID |
Now I just needed to get SS to recognize which is which.
// In Person.php static $belongs_many_many = array( 'Represents' => 'Company', 'Employers' => 'Company', ); // In Company.php static $many_many = array( 'Representatives'=> 'Person', 'Employees' => 'Person', );
As you can see, now Silverstripe won't know which is which. If I call $Person->Employers(), it won't know whether to look in Company_Representatives or Company_Employees for the right data.
The solution is to use dot notation on the class with <b>belongs_many_many</b> (and <u>NOT</u> the other) where the added parameter would be the reference key from many_many.
// In Person.php static $belongs_many_many = array( 'Represents' => 'Company.Representatives', 'Employers' => 'Company.Employees', ); // In Company.php static $many_many = array( 'Representatives'=> 'Person', 'Employees' => 'Person', );
P.S. This should be added to the docs. A dual has_one/has_many relationship is described there but it's not very clear and it doesn't state how to do it with many_many. :)