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.

Data Model Questions /

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

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


Go to End


3 Posts   15587 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, 36 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, 46 Posts

3 May 2013 at 3:19am

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

That should work.