Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Random image from album

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 1965 Views
  • Samba Sam
    Avatar
    Community Member
    85 Posts

    Re: Random image from album Link to this post

    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?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random image from album Link to this post

    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.

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Random image from album Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random image from album Link to this post

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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Random image from album Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random image from album Link to this post

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

    1965 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.