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.

DataObjectManager Module

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Random image from album


Reply

14 Posts   2129 Views

Avatar
Samba Sam

16 February 2010 at 8:02am (Last edited: 16 February 2010 8:15am), Community Member, 85 Posts

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

16 February 2010 at 8:25am 4085 Posts

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
Pigeon

16 February 2010 at 9:57am Community Member, 243 Posts

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

16 February 2010 at 10:23am 4085 Posts

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

Avatar
banal

16 February 2010 at 11:26am Community Member, 901 Posts

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

16 February 2010 at 11:52am 4085 Posts

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