Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Filter ArrayList Problem

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

Page: 1
Go to End
Author Topic: 271 Views
  • Johnny9
    Avatar
    Community Member
    15 Posts

    Filter ArrayList Problem Link to this post

    Hi, I want to filter ArrayList, but I don't know if I filter it correctly or even is it possible. Here is my code:

    public function getTopChildrenList() {

          $Childrens = $this->Children();      
             
          $limit = 4;
          
          $output = new ArrayList();

          foreach($Childrens as $C2) {
             $C2 = $C2->Children();   
           foreach($C2 as $C3) {               
                $C3 = $C3->Children()->filter('Created:GreaterThan', '2011-11-11')->limit($limit);
                foreach($C3 as $C4) {
                   $output->push($C4);
                 }               
             }
          }

          $list = $output;

          return $list;
       
    }

    I want to filter 7 days old items, but don't know how.. I if somebody know, please help.

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Filter ArrayList Problem Link to this post

    filter is the correct method to use and the filter looks good. You could debug the SQL for the DataList (to check the WHERE) by looking at $list->sql()

  • simon_w
    Avatar
    Forum Moderator
    467 Posts

    Re: Filter ArrayList Problem Link to this post

    To be able to use the SearchFilters, you need a DataList. You can achieve the same effect as Children() by using something like that:

    $C3 = $C3->stageChildren(false)->filter('Created:GreaterThan', '2011-11-11')->filterByCallback(function($item) {   
                return $item->canView();
             })->limit($limit);

  • Johnny9
    Avatar
    Community Member
    15 Posts

    Re: Filter ArrayList Problem Link to this post

    Thank you both for answers Hope I will get it working:)

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