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 /

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


Reply


3 Posts   2115 Views

Avatar
gaspra

Community Member, 8 Posts

19 April 2013 at 5:30pm

Hi!

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!

Avatar
Ironcheese

Community Member, 33 Posts

3 May 2013 at 1:26am

Hi,

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");

Cheers

Avatar
Sphere

Community Member, 44 Posts

3 May 2013 at 3:19am

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


That should work.