7912 Posts in 1355 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Random image from album
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
| Go to End | Next > | |
| Author | Topic: | 1733 Views |
-
Random image from album

12 February 2010 at 7:18pm
Hello,
On my home page I want to disply a large single testimonial image that randomly changes every time a visitor comes to the site.
Any suggestions on how to set that up. I can get as far as setting up testimonial data objects to create a gallery, I just don't know how to the display-a-singular-image randomly part.Thanks in advance,
Sam -
Re: Random image from album

12 February 2010 at 9:07pm Last edited: 12 February 2010 9:09pm
Hi
a quick way (some thing like this)
in your Controller
function MainShow(){
$ThumbURL = DataObject::get('YourDataObject', '','Rand()','','1');
return $ThumbURL;
}Template
<% control MainShow %>
<img src="$URL" /><% end_control %>
-
Re: Random image from album

13 February 2010 at 3:18am Last edited: 13 February 2010 3:19am
It's a little cleaner if you just return a single image. That way you don't need a control.
$ThumbURL = DataObject::get_one('YourDataObject', '',true,'Rand()');
<div>$MainShow</div>
-
Re: Random image from album

15 February 2010 at 11:47am
Hmmm, depending on the scale of your site, sorting by RAND() is really bad. It's slow and really could cause problems if you have a lot of objects in the DB.
This is some code i use to get one random object:
function getRandomObject() {
if ($do = DataObject::get('YourDataObject')) {
return $do->getRange(rand(0,$do->Count() - 1),1);
}
}
This uses php to pick a random object rather than MySQL. -
Re: Random image from album

15 February 2010 at 12:35pm
Looks like a lesser of two evils, here, guys. RAND() is hog on the DB side, no doubt, but getRange() isn't exactly the leanest function in the world, either. It creates a new set, and loops through the records it already has in memory, creating duplicates for the set matching your range.
If performance is really an issue, I think you might have to look outside of the Sapphire framework. For most sites, I think either of these solutions will be fine.
-
Re: Random image from album

16 February 2010 at 3:35am Last edited: 16 February 2010 3:37am
Hi Folks,
So I can't get any of the previously suggested solutions to work.
Solution #1 (PatJnr) , I get no image, no code in template.Solution #2 (Uncle Cheese) gives me the following error: Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'Testimonial'
Solution #3(Pigeon) I get a random number link (e.g., "#1") displaying with the following extra code inserted in the template:
<ul id="Menu1"><li onclick="location.href = this.getElementsByTagName('a')[0].href"><a href="">#1</a></li></ul>Here is my code:
mysite/code/Testimonial.php<?php
class Testimonial extends DataObject
{
static $db = array (
'Caption' => 'Varchar(50)'
);static $has_one = array (
'TestimonialPage' => 'TestimonialPage',
'TestimonialImage' => 'Image'
);public function getDOMThumbnail()
{
return $this->TestimonialImage()->CroppedImage(50,50);
}function getCMSFields()
{
return new FieldSet(
new ImageField('TestimonialImage'),
new TextField('Caption','Caption')
);
}
}
?>
********************
mysite/code/TestimonialPage.php<?php
class TestimonialPage extends Page {static $has_many = array (
'Testimonials' => 'Testimonial'
);function getCMSFields() {
$fields = parent::getCMSFields();
$manager = new ImageDataObjectManager(
$this,
'Testimonials',// Source name
'Testimonial',// Source class
'TestimonialImage',// File name on DataObject
array('Caption' => 'Caption'),
'getCMSFields'
);$manager->setAllowedFileTypes(array('jpg'));
$manager->setBrowseButtonText("Upload (JPG only)");
$fields->addFieldToTab("Root.Content.Testimonials",$manager);
return $fields;
}
} /* end SiteTree */class TestimonialPage_Controller extends Page_Controller {
/* example 2 */
function RandomTestimonial (){
$ImageURL = DataObject::get_one('Testimonial', '',true,'Rand()');
return $ImageURL; }
}
/* example 3 */
function getRandomObject() {
if ($do = DataObject::get('Testimonial')) {
return $do->getRange(rand(0,$do->Count() - 1),1);
}
}} /* end Controller */
?>************************
templates/layout/Testimonial.ss (example 2)
<div id="Mainbody">$RandomTestimonial</div>templates/layout/Testimonial.ss (example 3)
<div id="Mainbody">$getRandomObject</div>I wasn't sure if I was to give the object source name or class in the DataObject::get('Testimonial')) part. I tried doing both with no luck.
Anymore help would be greatly appreciated.
Thanks,
Sam -
Re: Random image from album

16 February 2010 at 4:13am
What you get by your function is the DataObject, not the image.
Either change your function to:function RandomTestimonial (){
if($dob = DataObject::get_one('Testimonial', '',true,'Rand()'))
return $dob->TestimonialImage();return null;
}Or write something like this in your template:
<div id="Mainbody">$RandomTestimonial.TestimonialImage</div>
| 1733 Views | ||
| Go to Top | Next > |




