21491 Posts in 5783 Topics by 2621 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1366 Views |
-
Getting random dataobjects

4 May 2011 at 8:24am
Hello,
I have ProductPages which has many ProductImages.I can get 3 random Product Page with the following code
function RandomProductPages($num=3) {
return DataObject::get("ProductPage", "", "RAND()", "", $num);
}I can also get random ProductImages with the following code
function RandomProductImage($num=1) {
return DataObject::get("ProductImage", "", "RAND()", "", $num);
}How to combine them, so that I can get 3 random product pages with 1 random image for each?
-
Re: Getting random dataobjects

10 May 2011 at 3:26am Last edited: 10 May 2011 3:29am
---- SOLVED ----
Ok, in the end I solved it. I post the code below, for future visitors
The main product image is selected randomly from child images. You can use it for custom galleries. The key point is, this function must be created in the subclass, not in the controller. Thanks to edk for this great tip. http://silverstripe.org/general-questions/show/15631?start=8#post302895#post302895
class ProductPage extends Page {
static $db = array(
);function CoverImage($num = 1) {
$Data = $this->getComponents('Photos', '', 'RAND()', '', $num);
return $Data;
}}
class ProductPage_Controller extends Page_Controller {
}
| 1366 Views | ||
|
Page:
1
|
Go to Top |

