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 /

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

Fetch multiples classe with DataObject::get

Go to End

13 Posts   2256 Views


Community Member, 130 Posts

11 November 2011 at 2:57pm

Ah okay. I think you want to use a TreeDropdownField - it should do most of the work for you. See example at under Class Details


Community Member, 26 Posts

11 November 2011 at 10:01pm

Edited: 12/11/2011 3:38am

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.

Community Member, 309 Posts

13 November 2011 at 12:39pm

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

return $set;


Community Member, 130 Posts

13 November 2011 at 1:03pm

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.


Community Member, 26 Posts

14 November 2011 at 8:53pm

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

Go to Top