Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Custom Included in Menu

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

Page: 1
Go to End
Author Topic: 555 Views
  • WebInt
    Avatar
    Community Member
    11 Posts

    Custom Included in Menu Link to this post

    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;
       }

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Custom Included in Menu Link to this post

    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");
    }

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