Jump to:

23377 Posts in 18296 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » Join on second many_many relation

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: 232 Views
  • Andre
    Avatar
    Community Member
    126 Posts

    Join on second many_many relation Link to this post

    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?

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