Jump to:

23368 Posts in 18143 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Getting random dataobjects

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 2163 Views
  • ayyurek
    Avatar
    Community Member
    41 Posts

    Getting random dataobjects Link to this post

    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?

  • ayyurek
    Avatar
    Community Member
    41 Posts

    Re: Getting random dataobjects Link to this post

    ---- 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 {

    }

    2163 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.