Jump to:

23550 Posts in 19336 Topics by 2890 members

General Questions

SilverStripe Forums » General Questions » DataObject::get - Filtering by 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: 581 Views
  • SheaDawson
    Avatar
    Community Member
    45 Posts

    DataObject::get - Filtering by many_many relation Link to this post

    Say I wanted to get a dataobjectset of all articles in a given category based on the relationship defined in the classes below... what would be the nicest way to do that in a DataObject::get() call?

    class Article extends DataObject{
       static $many_many = array(
          'Categories' => 'Category'
       );
    }

    class Category extends DataObject{
       static $belongs_many_many = array(
          'Articles' => 'Article'
       );
    }

    Currently I have just been doing a reverse lookup like

    DataObject::get_one('Category', 'URLSegment = 'cheese')->Articles();

    But that is not suitable in all cases

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