7913 Posts in 1355 Topics by 930 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, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 1454 Views |
-
DataObject::get_one called from in page control not refreshing

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
-
Re: DataObject::get_one called from in page control not refreshing

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?
-
Re: DataObject::get_one called from in page control not refreshing

18 February 2011 at 9:07am
It's a MySQL function.
-
Re: DataObject::get_one called from in page control not refreshing

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.
-
Re: DataObject::get_one called from in page control not refreshing

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.
-
Re: DataObject::get_one called from in page control not refreshing

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
-
Re: DataObject::get_one called from in page control not refreshing

29 March 2011 at 3:04am Last edited: 29 March 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
| 1454 Views | ||
|
Page:
1
|
Go to Top |


