Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

[Solved]Get dataobjectsets from all child pages


Go to End


3 Posts   4115 Views

Avatar
NickJacobs

Community Member, 148 Posts

8 April 2009 at 4:48pm

Edited: 08/04/2009 9:02pm

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

Avatar
Hamish

Community Member, 712 Posts

8 April 2009 at 8:10pm

Edited: 08/04/2009 8:11pm

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.

Avatar
NickJacobs

Community Member, 148 Posts

8 April 2009 at 9:00pm

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