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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Filter ArrayList Problem


Go to End


4 Posts   1847 Views

Avatar
Johnny9

Community Member, 35 Posts

25 October 2013 at 12:36am

Edited: 25/10/2013 12:39am

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 :)

Avatar
Willr

Forum Moderator, 5523 Posts

30 October 2013 at 7:12pm

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()

Avatar
(deleted)

Community Member, 473 Posts

30 October 2013 at 9:31pm

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

Avatar
Johnny9

Community Member, 35 Posts

13 November 2013 at 9:09am

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