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

Fetch multiples classe with DataObject::get

Go to End

13 Posts   1575 Views


10 November 2011 at 5:16am Community Member, 26 Posts

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.


Ryan M.

10 November 2011 at 6:50am Community Member, 309 Posts

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

This will combine different sets into one.


10 November 2011 at 8:36pm Community Member, 26 Posts

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


10 November 2011 at 9:06pm Community Member, 103 Posts

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.


10 November 2011 at 9:13pm Community Member, 103 Posts

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


10 November 2011 at 11:17pm Community Member, 26 Posts

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


10 November 2011 at 11:35pm Community Member, 103 Posts

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.


10 November 2011 at 11:53pm Community Member, 26 Posts

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
...more merges

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

Go to Top