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

Custom Included in Menu


Go to End


2 Posts   1554 Views

Avatar
WebInt

Community Member, 14 Posts

15 March 2011 at 11:47pm

Edited: 16/03/2011 1:49am

Hi,

I am trying to write a simple function which gets every page via the SiteTree, loops through each page and adds it to an array if included (added field in page class) = true.

I'm not particularly experienced with OO PHP and feel there's something fundamentally wrong with my code here.

function GetTopMenu() {
$AllPages = DataObject::get("SiteTree");
$IncludedPages = array();

foreach($AllPages as $Page) {//select the pages with "included = true"
array_push($IncludedPages, $Page);
}

return $IncludedPages;
}

This is just a test to see if I can take every page and put it into another array (not sure about putting objects into arrays, is that how it's done?), but returns nothing.

Any help would be greatly appreciated.

EDIT: Nevermind, got it.

function GetTopMenu() {
$AllPages = DataObject::get("SiteTree");
$OtherPages = new DataObjectSet();

foreach($AllPages as $Page) {//select the pages with "included = true"
if($Page->Included==true){
$OtherPages->push($Page);
}
}

return $OtherPages;
}

Avatar
Willr

Forum Moderator, 5523 Posts

16 March 2011 at 6:00pm

You shouldn't really iterate over all the pages again after your query unless you need to. If include is a column in your database just filter it using the WHERE cause.

function GetTopMenu() { 
   return DataObject::get("SiteTree", "\"Included\" = true");
}