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

DataObject::get - Filtering by many_many relation


Go to End


1107 Views

Avatar
sheadawson

Community Member, 49 Posts

5 September 2011 at 10:51pm

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