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.

General Questions /

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

Problems With "children()"


Reply


6 Posts   389 Views

Avatar
vovchyk

Community Member, 5 Posts

1 July 2013 at 8:07am

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 ?

Avatar
Willr

Forum Moderator, 5513 Posts

1 July 2013 at 6:17pm

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

$off = $this->Children();

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

Avatar
vovchyk

Community Member, 5 Posts

2 July 2013 at 9:15pm

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

Avatar
vovchyk

Community Member, 5 Posts

4 July 2013 at 9:36am

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

Avatar
kinglozzer

Community Member, 171 Posts

4 July 2013 at 8:15pm

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

Avatar
vovchyk

Community Member, 5 Posts

4 July 2013 at 8:19pm

Thanks, i will try next time.