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

Join on second many_many relation


Reply

252 Views

Avatar
Andre

20 June 2013 at 11:29pm Community Member, 126 Posts

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?