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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Random image from album


Go to End


14 Posts   3405 Views

Avatar
Samba Sam

Community Member, 85 Posts

16 February 2010 at 8:02am

Edited: 16/02/2010 8:15am

Thanks Banal! Works like a charm

So this is what I settled on:

In my TestimonialPage.php:

public function RandomTestimonial(){ 
	return DataObject::get_one('Testimonial','',true, 'RAND()');
	}

In my TestimonialPage.ss

<div id="Mainbody">$RandomTestimonial.TestimonialImage</div>

Thanks again everyone,
Sam

PS: What is the difference between using false and true in the above function?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

16 February 2010 at 8:25am

The third argument is for "cache".. I have no idea, really. I assume it means that if you run the same query again, if true, it will just look in memory for the result of the query rather than running it again. I always use false.

Avatar
dhensby

Community Member, 253 Posts

16 February 2010 at 9:57am

Hmm, sorting by Rand() wont make use of query cache, where as my previous method will.

Alternatively you could just make the user put the images in a specific folder and then grab one at random with PHP. Very quick.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

16 February 2010 at 10:23am

Yup! I agree, Pigeon. Just glob() it!!

Avatar
bummzack

Community Member, 904 Posts

16 February 2010 at 11:26am

I don't think you'll be looking at significant speed increases when DataObject count is below several hundred or even thousands. You can still tweak the method for performance when you identify this as bottleneck, otherwise it's just premature optimization.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

16 February 2010 at 11:52am

Sigh.. Here comes banal again with the voice of reason. Come on, man! Don't ruin our fun! :)

Go to Top