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

DataObject::get_one called from in page control not refreshing


Reply


7 Posts   1936 Views

Avatar
MrMuddy

Community Member, 13 Posts

18 February 2011 at 5:37am

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

Avatar
Ryan M.

Community Member, 309 Posts

18 February 2011 at 8:04am

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?

Avatar
UncleCheese

Forum Moderator, 4096 Posts

18 February 2011 at 9:07am

It's a MySQL function.

Avatar
MrMuddy

Community Member, 13 Posts

18 February 2011 at 9:43am

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.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

18 February 2011 at 2:18pm

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.

Avatar
MrMuddy

Community Member, 13 Posts

18 February 2011 at 11:58pm

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

Avatar
MrMuddy

Community Member, 13 Posts

29 March 2011 at 3:04am

Edited: 29/03/2011 3:18am

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