Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » DataObject::get_one called from in page control not refreshing

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 1834 Views
  • MrMuddy
    Avatar
    Community Member
    13 Posts

    DataObject::get_one called from in page control not refreshing Link to this post

    Hi All,

    I'm working on a site in Silverstripe 2.4.4

    I've got a bit of a complex holding page to build which draws various information from its children in the site tree, and also has some interspersed random images.

    In the page template itself, I have the following (snipped a bit to remove irrelevant junk):

    <% control Children %>

    <div class="foreground" id="fore$ID">
    <img class="galleryseat" src="/themes/blackcandy/images/bench.png" alt=""/>
    <img class="galleryobject" src="$Top.randomObject" alt=""/>
    </div>

    <% end_control %>

    Obviously $Top.randomObject is the function which selects a random image from one of the directories in the site. It is defined in the page controller :

    class GalleryMainPage_Controller extends ContentController {

       public static $allowed_actions = array (
       );

    public function randomObject() {
    //selects a random object from the pool and adds it to the page
    $randImg = DataObject::get_one("Image", "ParentID = 30", false, "RAND()" );
    return $randImg->Filename;
    }

    }

    Now, the function is working correctly and returns a random image from the specified directory, but the problem I am getting is that the image does not change with each loop in the Children control... so I end up with multiple copies of the same random image (if that makes sense !)

    I was hoping this would be a simple little function... Have I completely missed the point ?

    Any pointers appreciated.

    Cheers

    Tim

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: DataObject::get_one called from in page control not refreshing Link to this post

    DataObject::get_one will get the first most recently created object if you don't have any sorting parameters defined, which would be why it keeps fetching the same image over and over again.

    I'm not sure why you're using RAND() in there... that isn't a SS function that I know of. If it is, never seen it before. Where did you find it?

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObject::get_one called from in page control not refreshing Link to this post

    It's a MySQL function.

  • MrMuddy
    Avatar
    Community Member
    13 Posts

    Re: DataObject::get_one called from in page control not refreshing Link to this post

    Indeed... Rand() is a MySQL function and seems to work fine... as I say, I get a random image every time the page is refreshed... But I was hoping to get a random image every time the loop is iterated... that doesn't seem to be happening.

  • UncleCheese
    Avatar
    4085 Posts

    Re: DataObject::get_one called from in page control not refreshing Link to this post

    I have to say I'm surprised this is happening, because you are setting $cache = false in your third argument.

    Silverstripe caches queries by converting them to md5 hashes, so it seems to me that, if cache were set to true, you'd be running the same cached query every time (Silverstripe doesn't know that RAND() is going to return something different every time)

    But it is weird that even with cache set to false you're not getting anything new.. I thought that was the whole point of that flag.

  • MrMuddy
    Avatar
    Community Member
    13 Posts

    Re: DataObject::get_one called from in page control not refreshing Link to this post

    But it is weird that even with cache set to false you're not getting anything new.. I thought that was the whole point of that flag.

    That was my understanding too. I'm reluctant to ask if it's a bug, because I'm still new to SS and perfectly willing to accept that I might have done something stupid ;-)

    I'm on a tight deadline for the project, so I'll have to find another solution for now... but if I can figure anything out, I'll come back.

    Cheers

  • MrMuddy
    Avatar
    Community Member
    13 Posts

    Re: DataObject::get_one called from in page control not refreshing Link to this post

    Well... I've just stumbled across a very similar issue, and it looks like the culprit may be the caching engine rather than something specific to the DataObject....

    Rather than cross-posting and cluttering up this section, I've started a new thread in the templating section... as I think the template cache may be behind this.

    See here for details : http://www.silverstripe.org/template-questions/show/16374

    1834 Views
Page: 1
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.