Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Fetch multiples classe with DataObject::get

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

Page: 1 2
Go to End
Author Topic: 1328 Views
  • TomMiller
    Avatar
    Community Member
    26 Posts

    Fetch multiples classe with DataObject::get Link to this post

    Hi all,
    is it somehow possible to fetch more than one class with DataObject::get or can i add the result of one DataObejct to another? I am trying to adapt the MenuItem class from the MenuManager Module to fetch more than the standard page class.

    TIA Tom

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Fetch multiples classe with DataObject::get Link to this post

    All DataObject::get requests are returned as a DataObjectSet, so you could use $dataobjectset->merge($yourotherdataobjectset)

    This will combine different sets into one.

  • TomMiller
    Avatar
    Community Member
    26 Posts

    Re: Fetch multiples classe with DataObject::get Link to this post

    This works. Thanks Ryan.
    One more question if i may.
    What i am doing now is fetching a pagetypes and merging them - all 'manually'.

    Is there a function to return the different page type? I'd like to use this result to loop the merging. New page type will than be added 'automatically' to the merging process.

    Thanks again, Tom

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Fetch multiples classe with DataObject::get Link to this post

    If all your different page types extend from a Page class something like this might work? Untested

    $Pages = DataObject::get('Page', 'ClassName = 'BlogPage' OR ClassName = 'GalleryPage'); etc.

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Fetch multiples classe with DataObject::get Link to this post

    Not sure what you mean by "New page type will than be added 'automatically' to the merging process" - if all you want is to get every type of page there is then DataObject::get('Page'); or DataObject::get('SiteTree'); shold do it

  • TomMiller
    Avatar
    Community Member
    26 Posts

    Re: Fetch multiples classe with DataObject::get Link to this post

    All my types extend SiteTree.
    I already tried the query with DataObeject::get(Class1, Class2,...) and got an error message.
    With a SiteTree query i might get all valid (non-unique) ClassNames.
    To make them unique i would probably just have to figure out the right DataObject::get statement.
    With the unique ClassNames i could loop to get all DataObjects and merge them - if i knew how to...
    ...hope this makes things more understandable...

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: Fetch multiples classe with DataObject::get Link to this post

    I still don't quite get what you're after - but can you try something like

    $Pages = DataObject::get("SiteTree", "ClassName = 'Class1' OR ClassName = 'Class2");

    This means your asking for SiteTree objects with a $where statement that's saying 'of these classes only'.

    What are you trying to achieve exactly though in real-world terms? If you want a DataObjectSet of every page in your website I think simply doing DataObject::get("SiteTree") should give you that.

  • TomMiller
    Avatar
    Community Member
    26 Posts

    Re: Fetch multiples classe with DataObject::get Link to this post

    I am trying to adapt the MenuItem.php class from the menumanager module.
    This code:
    public function getCMSFields() {
    $pages = DataObject::get('Page');
    if ($pages) {
    $pages = $pages->toDropdownMap('ID', 'Title', '(Select one)', true);
    }
    works for the standard page class.
    What i did is this:
    $pages = DataObject::get('Page');
    $myClass = DataObject::get('MyClass');
    ... more classes
    $pages->merge($myClass)
    ...more merges

    This works but i want the function to automatically add all relevant classes to the dropdown.

    1328 Views
Page: 1 2
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.