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.

Data Model Questions /

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

Filter LessThanOrEqual and GreaterThanOrEqual don't work (SS 3.1.2)

Go to End

3 Posts   6163 Views


Community Member, 55 Posts

14 January 2014 at 6:42am

Hi experts,
I have created a news module to show articles on my website.
I want to show only news which are between my start- and end-date.
So I created:

$news = NewsArticle::get()->filter(array(
	'StartDate:LessThanOrEqual' => date('Y-m-d'),
	'EndDate:GreaterThanOrEqual' => date('Y-m-d')
	))->sort(array('Date' =>'DESC'));

which doesn't work.
Silverstripe debug says:

[User Error] Uncaught InvalidArgumentException: ExactMatchFilter does not accept LessThanOrEqual as modifiers
GET /index.php/de/aktuelles/?showqueries=1
Line 20 in /Applications/XAMPP/xamppfiles/htdocs/framework/search/filters/ExactMatchFilter.php
Why is Silverstripe using the ExactMatchFilter ?


$news = NewsArticle::get()->filter(array(
	'StartDate:LessThan' => date('Y-m-d'),
	'EndDate:GreaterThan' => date('Y-m-d')
	))->sort(array('Date' =>'DESC'));

works fine!
But doesn't give me the articles I do need.

Running SS 3.1.2

TIA Stephan


Community Member, 473 Posts

14 January 2014 at 8:42am

These were added to 3.1 after 3.1.2 was released, so they'll be in 3.1.3. Until then, you can do the opposite of what you're after to get the same result.

That is, filter('Field:LessThanOrEqual', value) becomes exclude('Field:GreaterThan' value). Likewise for GreaterThanOrEqual.


Community Member, 55 Posts

14 January 2014 at 10:23am

Edited: 14/01/2014 10:28am

Ok, thanks for the info.

So tried:

$news = NewsArticle::get()->exclude(array(
	'StartDate:GreaterThan' => date('Y-m-d'),
	'EndDate:LessThan' => date('Y-m-d')
	))->sort(array('Date' =>'DESC'));

But SilverStripe is fooling me,
instead of making

WHERE NOT(("NewsArticle_Live"."StartDate" > '2014-01-13') OR ("NewsArticle_Live"."EndDate" < '2014-01-13'))

the following SQL Query is generated:

WHERE (("NewsArticle_Live"."StartDate" <= '2014-01-13') OR ("NewsArticle_Live"."EndDate" >= '2014-01-13'))

this is wrong.

So I changed my code to

$news = NewsArticle::get()->filter(array(
	'StartDate:GreaterThan:Not' => date('Y-m-d'),
	'EndDate:LessThan:Not' => date('Y-m-d')
	))->sort(array('Date' =>'DESC'));

which makes the following nice SQL Query:

("NewsArticle_Live"."StartDate" <= '2014-01-13') AND ("NewsArticle_Live"."EndDate" >= '2014-01-13')