Jump to:

3460 Posts in 1064 Topics by 739 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: 1540 Views
  • JonoM
    Avatar
    Community Member
    103 Posts

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

    Ah okay. I think you want to use a TreeDropdownField - it should do most of the work for you. See example at http://api.silverstripe.org/2.4/forms/fields-relational/TreeDropdownField.html under Class Details

  • TomMiller
    Avatar
    Community Member
    26 Posts

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

    Thanks JonoM!
    For what i unterstand from the API (which might not be too much), the TreeDropdownfield ist not what i need.
    This $pages = $pages->toDropdownMap('ID', 'Title', '(Select one)', true); } function works well and seems to be similar to TreeDropdownfield.

    I am looking for an idea, or better a code howto, because i already have an idea and just don't know how to implement it.
    I would like to automate the process of fetching all classtype for pages, get the associated DataObjects and merge them together.

  • Ryan M.
    Avatar
    Community Member
    309 Posts

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

    You could store a comma separated list of classnames somewhere, maybe in the SiteConfig, so you can easily update it when you want to add more classnames. Then use that in a controller and iterate over each one, fetching their dataobjects and merging them.

    Something like:

    $classnames = array('Name1','Name2'); // in reality, this could be some code that fetches the stored classnames from where ever you're keeping them.

    $set = new DataObjectSet();
    foreach($classnames as $name) {
    $objs = DataObject::get("Page", "ClassName = '{$name}'");
    $set->merge($objs);
    }

    return $set;

  • JonoM
    Avatar
    Community Member
    103 Posts

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

    Hi Tom,

    I think you're making more work for yourself than you need to - am I right that you're trying to get a DataObjectSet that contains every page in the website?

    That shouldn't be a hard thing to do. Maybe you want to revise all your page type classes so they extend from Page, rather than SiteTree. So you have one class (Page extends SiteTree) then all others are (SomeCustomPage extends Page). Then if you used your function

    $pages = DataObject::get('Page');
    if ($pages) {
    $pages = $pages->toDropdownMap('ID', 'Title', '(Select one)', true);
    }

    You should get all the custom page types automatically because they extend from Page. However - if you're putting pages in to a dropdown field (which it looks like because you've included the optional blank value 'Select one') then I still think a TreeDropdownfield would be better because it mirrors your site tree structure and nests pages instead of listing them in one long list.

  • TomMiller
    Avatar
    Community Member
    26 Posts

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

    Thank you both for your help!
    Now i have a better idea what i should do. I'm going to give your code a try a asap.

    Thanks again, Tom

    1540 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.