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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

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


1047 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