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.

General Questions /

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

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

Join on second many_many relation


Go to End


659 Views

Avatar
Andre

Community Member, 146 Posts

20 June 2013 at 11:29pm

Hi, following case:
I have a Class, that is related as a many_many with two other classes. One of the many_many relations has an extra field for sorting.
Here is a simple Example in code:
<code>
class MyObject extends DataObject {
private static $db = array(
'Title' => 'Text',
'Active' => 'Boolean'
);

private static $belongs_many_many {
'Categories' => 'Category',
'Tags' => 'Tag'
}
}

class Tag extends DataObject {
private static $db = array(
'Title' => 'Text'
);

private static $many_many {
'MyObjects' => 'MyObject'
}
}

class Category extends DataObject {
private static $db = array(
'Title' => 'Text'
);

private static $many_many {
'MyObjects' => 'MyObject'
}

private static $many_many_extraFields=array(
'MyObjects'=>array(
'CategorySortOrder'=>'Int'
)
);

public function MyObjects(){
return $this->getManyManyComponents('MyObjects')->filter(array('Active' > 1))->sort('CategorySortOrder');
}
}
</code>
the last method "MyObjects()" of class Category returns all MyObjects that are active and that are related to the current category and orders them by CategorySortOrder.

Now my Question, how do I bring in the Tags Relation into here? I want to return the same data as above, but also the Objects returned should be related to one current Tag. Do I need to make an innerjoin and whicht table I have to join, as the Many-Many relation to tags is hold within a mapping table (tag_myobject)?

Can someone help me here?