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.

Archive /

Our old forums are still available as a read-only archive.

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

Get random object


Go to End


20 Posts   11864 Views

Avatar
moloko_man

Community Member, 72 Posts

2 September 2008 at 4:49pm

ok, I put the function in the right place and I'm calling it in the .ss page, but I'm getting this error:

Error
The website server has not been able to respond to your request.

I'll keep checking things, just to make sure I didn't leave something out.

Avatar
Double-A-Ron

Community Member, 607 Posts

2 September 2008 at 4:57pm

No idea. That's almost the exact code I have in use right now.

Cheers
Aaron

Avatar
moloko_man

Community Member, 72 Posts

2 September 2008 at 4:59pm

hmmm, what did you put in your .ss page?

Avatar
(deleted)

Community Member, 473 Posts

2 September 2008 at 5:01pm

moloko_man, append ?isDev=1 to you URL to get the error message.

Avatar
moloko_man

Community Member, 72 Posts

2 September 2008 at 5:08pm

wow, lots of errors :( still a noob at this, but trying to figure it out.
here is what I get:

FATAL ERROR: Bad class to singleton() - Products
At line 85 in /Applications/MAMP/htdocs/biomicro.com/sapphire/core/Core.php

user_error(Bad class to singleton() - Products,256)
line 85 of Core.php

singleton(Products)
line 1572 of DataObject.php

DataObject::get_one(Products,,,RAND())
line 18 of HomePage.php

HomePage_Controller->getRandomObject()

call_user_func_array(Array,Array)
line 404 of ViewableData.php

ViewableData->XML_val(getRandomObject,,1)
line 12 of .cache.Applications.MAMP.htdocs.biomicro.com.tutorial.templates.Layout.HomePage.ss

include(/private/var/folders/wO/wOacFITKFUaUHT-w02HAXk+++TI/-Tmp-/silverstripe-cache-Applications-MAMP-htdocs-biomicro.com/.cache.Applications.MAMP.htdocs.biomicro.com.tutorial.templates.Layout.HomePage.ss)
line 197 of SSViewer.php

SSViewer->process(HomePage_Controller)
line 189 of SSViewer.php

SSViewer->process(HomePage_Controller)
line 296 of Controller.php

Controller->defaultAction(index,Array)
line 267 of Controller.php

Controller->run(Array)
line 22 of ModelAsController.php

ModelAsController->run(Array)
line 27 of RootURLController.php

RootURLController->run(Array)
line 104 of Director.php

Director::direct(/)
line 158 of main.php

Avatar
(deleted)

Community Member, 473 Posts

2 September 2008 at 5:11pm

This is because you're trying to get a singleton of products instead of Product. Use:

function getRandomObject() { 
      return DataObject::get_one('Product', null, false, 'RAND()'); 
   } 

instead.

Avatar
moloko_man

Community Member, 72 Posts

2 September 2008 at 5:19pm

Edited: 02/09/2008 5:22pm

even with that change I still get a whole list of errors. in the get_one() function, what is the first item referring to? i.e. get_one('Product', null, false, 'RAND()'); 'Product' should be the section?

could it be that I have two sub levels to the Product page? I have the product page, then a page for each product and then pages for specs, publications, etc. should I just be sticking to one sub level?

Avatar
(deleted)

Community Member, 473 Posts

2 September 2008 at 5:21pm

Product is the name of the class you're trying to get one of. Do you have a Product class?