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.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

DataObject::get()->filter() many_many relation question

Go to End

3 Posts   10820 Views


Community Member, 8 Posts

19 April 2013 at 5:30pm


I have a simple Page with a many_many relation to a DataObject(s)

class FooPage extens Page {

static $many_many = array (
'MyFooObject' => 'FooObject'


Now I want to query Pages for display using FooPage::get() and then ->filter() it using the related object(s).

I want to return only Pages that have one or more related objects. Their content does not matter at all. I want to refuse Pages without a related object.

My SQL (and SilverStripe for that matter) experience is not too broad and so I cannot figure out how to do that. It seems trivial enough but I cannot find anything in the documentation.

Help is much appreciated!


Community Member, 36 Posts

3 May 2013 at 1:26am


i'm not sure if this is the best way but it worked in my case.
Try something like this:

// Get Pages with at least one FooObject Connection
$Pages = FooPage::get()->leftJoin('FooPage_MyFooObject', "FooPage.ID = FooPage_MyFooObject.FooPageID");



Community Member, 46 Posts

3 May 2013 at 3:19am

$pages = FooPage::get()->filter(array('MyFooObject.ID:GreaterThan' => 0));

That should work.