Jump to:

3430 Posts in 1057 Topics by 734 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [Solved]Get dataobjectsets from all child pages

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

Page: 1
Go to End
Author Topic: 2341 Views
  • NickJacobs
    Avatar
    Community Member
    144 Posts

    [Solved]Get dataobjectsets from all child pages Link to this post

    Hi, I have a 2 level site structure for products like this:

    Die Cast Toys
    ----Pull Back Vehicles
    ----Free Wheel Vehicles
    ----Guns

    etc

    When I am on a child page , ie Pull Back Vehicles I currently have all the dataobjects listing no probs see here

    I am using this code to do it:

    #####in ProductPage.php#########

    //get Product dataobjectset ready for pagination
    function ProductItems() {
    if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
    $SQL_start = (int)$_GET['start'];
    $doSet = DataObject::get(
    $callerClass = "Product",
    $filter = "`ProductPageID` = '".$this->ID."'",
    $sort = "",
    $join = "",
    $limit = "{$SQL_start},15"
    );

    return $doSet ? $doSet : false;
    }

    What I am having trouble with is figuring out how to get ALL child dataobjects. So if I go to 'Die Cast Toys' I need to see all dataobjects from the 3 child pages.

    I'm not sure if I need to find and then loop through all the child id's within a function??? or some other way....

    function AllChildProductItems() {
    if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) $_GET['start'] = 0;
    $SQL_start = (int)$_GET['start'];
    $doSet = DataObject::get(
    $callerClass = "Product",

    ##### find and loop here???? ######
    $filter = "`ProductPageID` = '".$this->ID."'",
    ###########################
    $sort = "",
    $join = "",
    $limit = "{$SQL_start},15"
    );

    return $doSet ? $doSet : false;
    }

    Thanks for any suggestions in advance!!

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: [Solved]Get dataobjectsets from all child pages Link to this post

    Heya,

    Pages are 'Heirarchical', so you should be able to do:

    $pageIds = $this->getDescendantIDList();

    This should create an array of ID's, with the current page and any descendant page ID's.

    You could then do:

    $doSet = DataObject::get(
       "Product",
       "`ProductPageID` IN (" . implode(",", $pageIDs) . ")",
       "",
       "",
       "{$SQL_start},15"
    );

    This should return all Product objects that have a ProductPageID that is either the current page's ID, or any child page of the current page.

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: [Solved]Get dataobjectsets from all child pages Link to this post

    Thanks Hamish, thats great.....it works perfectly:)

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