Jump to:

3429 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject::get()->filter() many_many relation question

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

Page: 1
Go to End
Author Topic: 1720 Views
  • gaspra
    Avatar
    Community Member
    8 Posts

    DataObject::get()->filter() many_many relation question Link to this post

    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!

  • Ironcheese
    Avatar
    Community Member
    33 Posts

    Re: DataObject::get()->filter() many_many relation question Link to this post

    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

  • Sphere
    Avatar
    Community Member
    44 Posts

    Re: DataObject::get()->filter() many_many relation question Link to this post

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


    That should work.

    1720 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.