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.

Data Model Questions /

Custom Included in Menu


Reply


2 Posts   687 Views

Avatar
WebInt

Community Member, 11 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, 5513 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");
}