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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Fetch multiples classe with DataObject::get


Go to End


13 Posts   4294 Views

Avatar
TomMiller

Community Member, 26 Posts

10 November 2011 at 5:16am

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

Avatar
Ryan M.

Community Member, 309 Posts

10 November 2011 at 6:50am

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

This will combine different sets into one.

Avatar
TomMiller

Community Member, 26 Posts

10 November 2011 at 8:36pm

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

Avatar
JonoM

Community Member, 130 Posts

10 November 2011 at 9:06pm

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.

Avatar
JonoM

Community Member, 130 Posts

10 November 2011 at 9:13pm

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

Avatar
TomMiller

Community Member, 26 Posts

10 November 2011 at 11:17pm

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

Avatar
JonoM

Community Member, 130 Posts

10 November 2011 at 11:35pm

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.

Avatar
TomMiller

Community Member, 26 Posts

10 November 2011 at 11:53pm

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.

Go to Top