Jump to:

22999 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Problems With "children()"

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 244 Views
  • vovchyk
    Avatar
    Community Member
    5 Posts

    Problems With "children()" Link to this post

    I have this methods in class:

    public function getOffServers(){
          $off = $this->Children()->filter('Official', 1);
          Debug::show($off);
          return $off;
          }
       public function getNotOffServers(){
          $noff = $this->Children()->filter('Official', 0);
          Debug::show($noff);
          return $noff;
          }
       public function getServers(){
          $s = $this->Children();
          Debug::show($s);
          return $s;
          }

    'getOffServers' - i am calling this one first in template, this method works ok but if call "getNotOffServers" i get emply list and when i am calling 'getServers' - i have the same list as 'getOffServers' (not all children), if i am using in template "<% loop children %>" after 'getOffServers' i have just part of chilren (the list is the same as for 'getOffServers' ), do you have ideas ?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Problems With "children()" Link to this post

    Try cloning the DataList to a new variable before adding filters.

    $off = $this->Children();

    return $off->filter('Official', 1);

  • vovchyk
    Avatar
    Community Member
    5 Posts

    Re: Problems With "children()" Link to this post

    Thanks for reply but it doent help(
    Output is the same.

  • vovchyk
    Avatar
    Community Member
    5 Posts

    Re: Problems With "children()" Link to this post

    I have found solution but it look not so great, i have used this code-

    public function getOffServers(){
          $output = new ArrayList();
          $createdOptionsArrayData = false;
          $children = $this->Children();
          foreach ($children as $server) {
             if ($server->Official == '1'){
                $output->push(new ArrayData(array(
                'Title' => $server->Title
                )));
             }
          }
          //Debug::Show($output);
          return $output;
          }

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: Problems With "children()" Link to this post

    Have you tried perhaps:

    <% loop Children.Exclude('Official', 0) %>
    <!-- Loop official servers -->
    <% end_loop %>

    <% loop Children.Exclude('Official', 1) %>
    <!-- Loop not official servers -->
    <% end_loop %>

  • vovchyk
    Avatar
    Community Member
    5 Posts

    Re: Problems With "children()" Link to this post

    Thanks, i will try next time.

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